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".
Given the (3,3) matrix below.
>>> 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.
If I use the index 3
I can get the respective value from the matrix, using:
>>> 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 (i.e. the index once flattened) corresponds to an x and y index in the shaped matrix like so:
Position from start of flat: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 ... etc
y index in your matrix m : 0 | 0 | 0 | 1 | 1 | 1 | 2 | 2 | 2 | 3 | 3 ... etc
x index in your matrix m : 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 ... etc
So a pattern maxes itself apparent. Consider your problem in reverse.
Given the row and column index, the 'book' (i.e. 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. 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.
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
Similarly looking at id and y we see that the value stays the same for 3 elements, increasing by one in a periodic way. 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.)
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. Drawing it out and labelling the coordinates made it become apparent so that is what I aimed to do here.