Find numeric associated position of ordered pair in matrix

23 Views Asked by At

I've got a matrix in which each element is an ordered pair (a;b). a represents the line in which I'm positioned, and b represents the element in that same line. For example:

Matrix:
    1     2     3
1 (1;1) (1;2) (1;3)
2 (2;1) (2;2) (2;3)
3 (3;1) (3;2) (3;3)

What I need is to get the numeric position of the pair (i.e, (1;1) = 1, (3;2) = 8). I've deduced a possible solution to the problem being:

X = line lenght
(a;b) = x*a - (x - b)

For some examples the formula I've deduced it works, however, I don't know if it's all correct and if could be a case in which it won't work.

I would like to know if there is a "standard" way to do this, and if my formula is fine and why it does work.

1

There are 1 best solutions below

0
On BEST ANSWER

Suppose there are $m$ rows and $n$ columns.

Given $(i,j)$, it is at the $i$-th row and $j$-th column.

The last element of the $(i-1)$-th row is index $(i-1)n$ since each row has $n$ elements.

For the $i$-th row and $j$-column, we increment the index count by $j$.

Hence the formula $(i-1\cdot )n+j=i\cdot n-(i-j)$, that is your formula works.