Interpreting a Vector as a Matrix.

32 Views Asked by At

Suppose you have a $N$ x $N$ grid, or matrix. If you wanted to somehow "stretch it", turning it into an array of sorts, such that now every element is to the right or left of each other, like in a vector. But you would like to keep referring to each element by their original reference -- that is row and col. What you would do it divide it's index in the vector by $N$. It's row would be index//N (integer division) and the column would be the remainder.

My question is: what if the grid were $N$ x $M$, resulting in an array of size N*M, how would you get the corresponding position of each element in the grid?

1

There are 1 best solutions below

0
On

The way to obtain the position of element $a_{ij}$ in a matrix of size $m\times n$ would be to compute

$$ m(j-1) + i. $$

This is if you take the columns of the matrix and stack them as a single column going from left to right.