简体   繁体   English

使用“地板除法”和“模数”运算符索引矩阵

[英]Indexing a matrix using “floor division” and “modulus” operators

I saw a python code where the value from a matrix could be grabbed using a "index" and the both python operators "floor division" and "modulus".我看到了一个 python 代码,其中可以使用“索引”和 python 运算符“地板除法”和“模数”来获取矩阵中的值。

Given the (3,3) matrix below.给定下面的 (3,3) 矩阵。

>>> m = np.array([['0>','1>','2>'],['3>','4>','5>'],['6>','7>','8>']])
>>> m
array([['0>', '1>', '2>'],
       ['3>', '4>', '5>'],
       ['6>', '7>', '8>']], dtype='<U2')

If we "flat" the given matrix we will have:如果我们“扁平化”给定的矩阵,我们将拥有:

>>> m.reshape(-1)
array(['0>', '1>', '2>', '3>', '4>', '5>', '6>', '7>', '8>'], dtype='<U2')

Let's suppose that I want to read the value '3>' that is the value in the 4th position in the array.假设我想读取值“3>”,即数组中第 4 个 position 中的值。

If I use the index 3 I can get the respective value from the matrix, using:如果我使用索引3 ,我可以从矩阵中获取相应的值,使用:

>>> idx = int(np.where(m.reshape(-1) == '3>')[0])
>>> idx
3
>>> x = idx // m.shape[0]
>>> y = idx % m.shape[0]
>>> 
>>> m[x][y]
'3>'
>>>

I can't see how this work.我看不出这是如何工作的。

What is the explanation for that?对此有何解释?

If you read the array like a book (left to right, row by row from top to bottom), then each characters position from the start (ie the index once flattened) corresponds to an x and y index in the shaped matrix like so:如果你像一本书一样阅读数组(从左到右,从上到下逐行),那么从一开始的每个字符 position (即一旦变平的索引)对应于形状矩阵中的xy索引,如下所示:

Position from start of flat: 0 | Position 从平面开始:0 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 6 | 6 | 7 | 7 | 8 | 8 | 9 | 9 | 10... etc 10...等

y index in your matrix m : 0 |矩阵中的y索引m : 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 3 | 3 | 3... etc 3...等

x index in your matrix m : 0 |矩阵中的x索引m : 0 | 1 | 1 | 2 | 2 | 0 | 0 | 1 | 1 | 2 | 2 | 0 | 0 | 1 | 1 | 2 | 2 | 0 | 0 | 1... etc 1...等

So a pattern maxes itself apparent.因此,一种模式本身就很明显。 Consider your problem in reverse.反过来考虑你的问题。

Given the row and column index, the 'book' (ie flattened) index is i = x + ny where n is the number of elements in a row, in your case 3. This general pattern holds anywhere.给定行和列索引,“书”(即展平)索引为i = x + ny ,其中 n 是一行中的元素数,在您的情况下为 3。这种一般模式适用于任何地方。 This equation doesn't really answer your question in full, though hopefully it sheds some light.这个等式并没有真正完整地回答你的问题,但希望它能够提供一些启示。

We can construct two more equations looking at 2 rows at a time in the collection of 3 rows above.我们可以在上面的 3 行集合中构造另外两个方程,一次查看 2 行。

Looking at id and x we see that dividing id by the number of elements to a row consistently yields the x address as the remainder查看idx我们看到将 id 除以元素的数量到一行始终会产生 x 地址作为余数

Similarly looking at id and y we see that the value stays the same for 3 elements, increasing by one in a periodic way.类似地查看idy ,我们看到 3 个元素的值保持不变,周期性地增加 1。 That's just what you get if you keep taking the floor function of sequential integers with respect to 3 (and of course this generalizes.)这就是如果你继续发言 function 关于 3 的顺序整数(当然这是概括的),你会得到什么。

I hope this answers your question.我希望这回答了你的问题。 I learned this logic when constructing a chess board in Excel, and wanted to store pieces with respect to their 'flat index' but computing move possibilities was so much easier with respect to x/y coordinates.我在 Excel 中构建棋盘时学到了这个逻辑,并且想根据它们的“平面索引”来存储棋子,但是相对于 x/y 坐标计算移动可能性要容易得多。 Drawing it out and labelling the coordinates made it become apparent so that is what I aimed to do here.将其绘制出来并标记坐标使其变得明显,这就是我在这里的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM