# Matrix location by indices?

The formula for location number by successive rows for an $n$ by $m$ matrix is

$f(x,y)=m(x-1)+y$

That is, for example a matrix with $n=3, m=4$ \begin{bmatrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \end{bmatrix}

for example, $f(2,3)=4(2-1)+3=7$

I've found this out just a while ago from trying to not use loops in code to get the same answer, but it's been bothering me why $n$ is not in this formula? Sorry if the question is too simple, i was just curious as to why that is. Is it possible to make this a function of $n$ as well?

The reason why $n$ doesn't appear in $f(x,y)=m(x-1)+y$ is because your matrix is written left-to-right and then top-to-bottom. With $f(x,y)=m(x-1)+y$, when you jump to the next row ($x \mapsto x+1$), the entry in the matrix increases by $m$; however when you jump to the next column ($y \mapsto y+1$) the entry in the matrix increases only by $1$.

Had you written your matrix top-to-bottom and then left-to-right, i.e. $$\begin{pmatrix} 1 & 4 & 7 & 10 \\ 2 & 5 & 8 & 11 \\ 3 & 6 & 9 & 12 \end{pmatrix}$$ then the formula would have been $g(x,y)=x+(n-1)y$.

If you wanted to make your function give a one-to-one correspondence between the numbers $1,2,\cdots,mn$ and the entries of the $n \times m$ matrix, you'd need to specify $1 \le x \le n$ and $1 \le y \le m$ in the definition of the function; so really both numbers, $m$ and $n$, do implicitly appear.

The variable $m$ occurs in the formula because it lets you know how many entries are in the previous rows (which you need to know in order to compute the current entry). The variable $n$ does not occur in the formula because you do not need to know the total number of rows, you only need to know what row you're currently on.

If you want $n$ to be in there you can think of $n$ as being in the constraints for the domain of your formula. Your formula is only valid when $1 \leq x \leq n$.