I am trying to rearrange the value inside grid. For example, the below code
\documentclass[tikz,border=7mm]{standalone}
\begin{document}
\tikz\draw grid(4,4)foreach[count=~]\l in{1,...,16}
{({.5+mod(~-1,4},{3.5-div(~-1,4})node{\l}};
\end{document}
gives a 4*4 grid where each box has a number from 1 to 16. like-
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
How can I perform the same thing using two for loop such that the grid becomes-
1 2 2 1
3 4 4 3
3 4 4 3
1 2 2 1
For 8 rows and 8 column, each quadrant should start with 1 and end with 16.
1 2 3 4 4 3 2 1
5 6 7 8 8 7 6 5
9 10 11 12 12 11 10 9
13 14 15 16 16 15 14 13
13 14 15 16 16 15 14 13
9 10 11 12 12 11 10 9
5 6 7 8 8 7 6 5
1 2 3 4 4 3 2 1
Also , How can I print inside grid such as-
a=1 a=2 a=2 a=1
a=3 a=4 a=4 a=3
a=3 a=4 a=4 a=3
a=1 a=2 a=2 a=1
I am trying to construct the general code that is valid for x rows and y columns instead of 4 rows and 4 columns.
I found the above code from here.






\def\myarray{{1,2,2,1,3,4,4,3,3,4,4,3,1,2,2,1}}. See page 927 of PGF manual. – John Kormylo Apr 09 '17 at 20:40