# Get matrix coords from a single index and matrix dimensions

Having a set of ordered numbers $$\{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 \}$$ If you put them in a matrix so the index is at the same time the content

Matrix dimension $=$ row x col $= 3\times 4$ $$\begin{bmatrix} 1 &2 &3 & 4\\ 5 &6 & 7 & 8\\ 9 &10 &11 &12 \end{bmatrix}$$ examples of getting the coordinates from $n=$index

$f_{row}(n)$ get the row, $f_{col}(n)$ get the column

$f_{row}(2) = 1, f_{col}(2) = 2$

$f_{row}(10) = 3, f_{col}(10) = 2$

$f_{row}(12) = 3, f_{col}(12) = 4$

They seems to be complex functions, i.e. (non-linear, bounded, etc..)

How are $f_{row}$ and $f_{col}$ defined?

I think a software equation, but it has functions like

$\operatorname{int}(\cdot), \operatorname{floor}(\cdot),\operatorname{mod}(\cdot)\ldots$ or worst a sequenced algorithm..

I don't know if there is some "math way" to do this, I mean with "basic operations" an "arithmetic" solution. Of course you could say "Floor and ceiling functions are math and even algorithms!" but simply put: Is there any series expansions/ other path for doing this? I would like to "relate index with dimentions", in some equation.

-
I think you got the numbers reversed for frow(12) and fcol(12). –  Calle Sep 13 '11 at 22:13
@Calle you were right, corrected. –  Hernán Eche Sep 14 '11 at 0:17
Also you have this –  user13838 Sep 14 '11 at 11:19

Well... In your case, with the $3 \times 4$-matrix you can express your functions as $$f_\textrm{row}(n) = \lceil n / 4 \rceil$$ where $\lceil x \rceil$ means the smallest integer larger or equal to $x$ ($x$ rounded up), and $$f_\textrm{col}(n) = ((n-1) ~ \% ~ 4) + 1$$ that is, the remainder of $(n-1)/4$ plus one.

If you have a $k \times m$-matrix: $$f_\textrm{row}(n) = \lceil n / m \rceil$$ and $$f_\textrm{col}(n) = ((n-1) ~ \% ~ m) +1$$

EDIT: Sorry, I did not read the last part of your question carefully enough (I assumed you wanted to use the expressions in some kind of computer program).

Given a step function $h$: $$h(x) = \begin{cases} 0 & x < 0 \\ 1 & x \geq 0 \end{cases}$$ you can express $f_\textrm{row}(n)$ for a $k \times m$-matrix as $$f_\textrm{row}(n) = \sum_{i = 0}^k h(n - m i-1)$$ where you can change the upper limit to $\infty$ and it will hold for all $k$.

The first values of $f_\textrm{col}(n)$ is $1, 2, 3, \dots, m, 1, \dots$ so we can set $$f_\textrm{col}(n) = \sum_{i=1}^m h(n-i) ~~ \textrm{for} ~~ n = 1, \dots, m$$ and then we want to make this periodic with period $m$. The easiest way is to use a modulo operation, but it can be achieved by introducing more sums: $$f_\textrm{col}(n) = \sum_{i=1}^m h(n- i) + \sum_{j=1}^k \left( \sum_{i=1}^m h(n-i-jm) - m \cdot h(n-jm-1) \right)$$ Once again you can set $k = \infty$ and still get the correct result for all $k$.

EDIT2: Here is another idea for $f_\textrm{col}$. Since $$f_\textrm{col}(n) = n ~~ \textrm{for} ~~ n = 1, \dots, m$$ you can probably get $f_\textrm{col}$ as a sawtooth wave with frequency $\frac{1}{m}$ and amplitude $m$. One probably should be careful when doing this to make sure that for example $f_\textrm{col}(m) = m$ and not $f_\textrm{col}(m) = 0$.

EDIT3: A sawtooth wave-like $f_\textrm{col}$ can be expressed as:

$$f_\textrm{col}(n) = n - m \sum_{i=1}^k h(n - m i - 1) = n - m(f_\textrm{row}(n) - h(n-1))$$

when $f_\textrm{row}$ is taken as the sum of $h$-functions above.

-
Well.. this is a nice answer +1, nonetheless I was trying to avoid using "$\lceil~~\rceil$" and "$~ \% ~$", is good to see in the equation the independence of $k$. I was trying to "mix" these equation by myself, but I see it was out of my scope and perhaps it was already made, upload.wikimedia.org/math/d/4/9/… upload.wikimedia.org/math/8/8/4/… –  Hernán Eche Sep 14 '11 at 0:39
@Hernán Eche, I added another possible solution. I hope this helps more. –  Calle Sep 14 '11 at 11:06
although $h$ is not linear and I also realize that $\sum_{}$'s are like algorithm loops, it's a great answer, I will test it, selected, thanks –  Hernán Eche Sep 14 '11 at 12:00