### 2.80 Convert 2D matrix to show the location and values

Given \begin {align*} A =& \left ( {\begin {array}{cccc} 41 & 45 & 49 & 53 \\ 42 & 46 & 50 & 54 \\ 43 & 47 & 51 & 55 \\ 44 & 48 & 52 & 56 \\ \end {array}} \right ) \end {align*}

Generate the matrix

$\left ( {\begin {array}{ccc} 1 & 1 & 41 \\ 2 & 1 & 42 \\ 3 & 1 & 43 \\ 4 & 1 & 44 \\ 1 & 2 & 45 \\ 2 & 2 & 46 \\ 3 & 2 & 47 \\ 4 & 2 & 48 \\ 1 & 3 & 49 \\ 2 & 3 & 50 \\ 3 & 3 & 51 \\ 4 & 3 & 52 \\ 1 & 4 & 53 \\ 2 & 4 & 54 \\ 3 & 4 & 55 \\ 4 & 4 & 56 \\ \end {array}} \right )$

Which gives at each row, the location and the value in the original matrix.

Mathematica

mat = {{41, 45, 49, 53}, {42, 46, 50, 54}, {43, 47, 51, 55}, {44, 48, 52, 56}};
Flatten[MapIndexed[Flatten[{{#2[[2]], #2[[1]]}, #1}] &, Transpose@mat, {2}],1]



Another way

mat = {{41, 45, 49, 53}, {42, 46, 50, 54}, {43, 47, 51, 55}, {44, 48, 52, 56}};
{nRow, nCol} = Dimensions[mat];
idx = Flatten[Table[{i, j}, {j, nCol}, {i, nRow}], 1]; (*similar to ind2sub*)
val = Extract[mat, idx];
MapThread[Flatten[{#1, #2}] &, {idx, val}] (* similar to [I;J;A(:)']' *)



But I think the simplist is to use Table

mat = {{41, 45, 49, 53}, {42, 46, 50, 54}, {43, 47, 51, 55}, {44, 48, 52, 56}};
{nRow, nCol} = Dimensions[mat];
Table[{i, j, mat[[i, j]]}, {j, nCol}, {i, nRow}];
Flatten[%, 1]



Matlab

A = [ 41 45 49 53;
42 46 50 54;
43 47 51 55;
44 48 52 56];
[I,J] = ind2sub(size(A),1:numel(A));
X     = [I;J;A(:)']'



gives

X =
1     1    41
2     1    42
3     1    43
4     1    44
1     2    45
2     2    46
3     2    47
4     2    48
1     3    49
2     3    50
3     3    51
4     3    52
1     4    53
2     4    54
3     4    55
4     4    56



Maple

A := Matrix([[41,45,49,53],[42,46,50,54],[43,47,51,55],[44,48,52,56]]);



A= \left [ \begin {array}{cccc} 41&42&43&44\\ \noalign {\medskip }45&46&47&48\\ \noalign {\medskip }49&50&51&52\\ \noalign {\medskip }53&54&55&56 \end {array} \right ]

[seq(seq([i,j,A(i,j)],j=1..4),i=1..4)]:
indx:=Matrix(%);



\left [ \begin {array}{ccc} 1&1&41\\ \noalign {\medskip }1&2&45 \\ \noalign {\medskip }1&3&49\\ \noalign {\medskip }1&4&53 \\ \noalign {\medskip }2&1&42\\ \noalign {\medskip }2&2&46 \\ \noalign {\medskip }2&3&50\\ \noalign {\medskip }2&4&54 \\ \noalign {\medskip }3&1&43\\ \noalign {\medskip }3&2&47 \\ \noalign {\medskip }3&3&51\\ \noalign {\medskip }3&4&55 \\ \noalign {\medskip }4&1&44\\ \noalign {\medskip }4&2&48 \\ \noalign {\medskip }4&3&52\\ \noalign {\medskip }4&4&56\end {array} \right ]