Finding mathematical relation of matrices with reverse indices I am designing a simple game, I have faced this problem to get the mathematical relation between two kind of tables:
MATRIX A

MATRIX B

As you can see the table A (or Matrix A) is the normal table and table B is the reveresd and the next number at the end of each row is continued on a right to left or left to right pattern.
If the last row of table B (which actually is the first row) ends for example with number 8 then immideatly above number 8 we will have 9.
The pictures are just examples, this table can be in any NxM fashion.
What I do at the moment is having a hard coded map for some low numbers of NxM that translates the Table A to Table B. Example:
A -> B

If i feed 1 in the function, I get 9
If I feed 8 in the function, I get 5

B -> A
1 will give 9
11 will give 3

I was wondering if a mathematical expression can be found to make this conversion from A to B efficent and global!
 A: If $A=(a[i,j])_{1\leq i\leq n,1\leq j\leq m}$ is a given $n\times m$ matrix, then define
$B=T(A)=(b[i,j])_{1\leq i\leq n,1\leq j\leq m}$ by the formula
$$
b[i,j]=a[n+1-i,\frac{1-(-1)^{n+i}}{2}(m+1)+(-1)^{n+i}j]
$$
The reverse transformation $A=S(B)$ is given by
$$
a[i,j]=b[n+1-i,\frac{1+(-1)^{i}}{2}(m+1)-(-1)^{i}j]
$$
A: Let's first write down formulae for the elements of each matrix based on their location in the matrix.
For $A$ we simply have
$$A_{i,j}=M(i-1)+j$$
as each row contains $M$ consecutive elements and the count proceeds from left to right and top to bottom.
$B$ is a little more complicated. We get
$$B_{i,j}=
\begin{cases}
(N-i)M+j & \text{if } i \equiv N\pmod2\\ 
(N-i)M+(M-j+1) & \text{if }i\not\equiv N\pmod2
\end{cases}$$
The $(N-i)$ part comes from the start of the count being at the bottom of the matrix, while the $j$ or $(M-j+1)$ parts come from the direction of the count on the row, which in turn depends on the parity of the row number with respect to the parity of $N$. 
Letting $f$ be the map from $A$ to $B$, we want $f(A_{i,j})=B_{i,j}$. Now given $x$ as a valid element of $A$, we can compute $i$ and $j$, namely $i=g(x)=\left\lceil\dfrac{x}{M}\right\rceil$ and $j=h(x)=x\pmod{M}$ (taking $h(x)=M$ instead of $h(x)=0$ in the case $M|x$).
Hence we have 
$$f(x)=
\begin{cases}
(N-g(x))M+h(x) & \text{if } g(x) \equiv N\pmod2\\ 
(N-g(x))M+(M-h(x)+1) & \text{if }g(x)\not\equiv N\pmod2
\end{cases}$$
where $g(x)=\left\lceil\dfrac{x}{M}\right\rceil$ and $h(x)=\begin{cases}x\pmod{M} &\text{if } M\nmid x\\ M &\text{if }M\mid x\end{cases}$
Similarly, we can compute $f^{-1}$, the map from $B$ to $A$. Computing $i$ and $j$ again, we get $i=p(x)=\left\lceil\dfrac{NM-x+1}{M}\right\rceil=\left\lceil N-\dfrac{x-1}{M}\right\rceil$, and 
$j=q(x)=\begin{cases}
x\pmod{M} & \text{if } p(x)\equiv N\pmod{2}\text{ and }M\nmid x \\
M & \text{if } p(x)\equiv N\pmod{2}\text{ and }M\mid x \\
M-x+1\pmod{M} & \text{if } p(x)\not\equiv N\pmod{2}\text{ and }M\nmid (x-1) \\
M & \text{if } p(x)\not\equiv N\pmod{2}\text{ and }M\mid (x-1)
\end{cases}$
and at long last we have
$$f^{-1}(x)=M(p(x)-1)+q(x)$$
