# Doing ArrayReshape in Mathematica doesn't give desired results

I have an array like this for example,

a = ArrayReshape[Range[16], {4, 4}]


\begin{align} \left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \\ \end{array} \right) \end{align} Now, I want to reshape this further more and get this kind of an array:

\begin{align} \left( \begin{array}{cc} \left( \begin{array}{cc} 1 & 2 \\ 5 & 6 \\ \end{array} \right) & \left( \begin{array}{cc} 3 & 4 \\ 7 & 8 \\ \end{array} \right) \\ \left( \begin{array}{cc} 9 & 10 \\ 13 & 14 \\ \end{array} \right) & \left( \begin{array}{cc} 11 & 12 \\ 15 & 16 \\ \end{array} \right) \\ \end{array} \right) \end{align}

But, when I do ArrayReshape, I get this

b = ArrayReshape[a, {2, 2, 2, 2}]


\begin{align} \left( \begin{array}{cc} \left( \begin{array}{cc} 1 & 2 \\ 3 & 4 \\ \end{array} \right) & \left( \begin{array}{cc} 5 & 6 \\ 7 & 8 \\ \end{array} \right) \\ \left( \begin{array}{cc} 9 & 10 \\ 11 & 12 \\ \end{array} \right) & \left( \begin{array}{cc} 13 & 14 \\ 15 & 16 \\ \end{array} \right) \\ \end{array} \right) \end{align} But Partition gives the desired shape,

Partition[a, {2, 2}]
{{{{1, 2}, {5, 6}}, {{3, 4}, {7, 8}}}, {{{9, 10}, {13, 14}}, {{11, 12}, {15, 16}}}}


It would be nicer to reshape from numerical tensors to different shape and do tensor operations, for which reshape function gives the desired result in julia and python, for example in Python I can do,

a = np.random.rand(12,12)
b = a.reshape(3,4,3,4)
np.all(b[0,:,0,:] == a[0:4,0:4]) # true
np.all(b[1,:,1,:] == a[4:8,4:8]) # true


Try this:

Map[Transpose, ArrayReshape[a, {2, 2, 2, 2}]] // MatrixForm


Or using Partition:

Partition[a, {2, 2}] // MatrixForm


• Nice. Even simpler Transpose[ ... , {1, 3, 2}] Jun 3, 2022 at 7:05

The array in matrix-form can sometimes be misleading, if you call the indexes of the tensor properly, you'll see that it's similar to other languages that use Row-Major ordering (e.g., Python) to store arrays. In Mathematica, you can do this

a1 = ArrayReshape[Range[16], {4, 4}];
a = ArrayReshape[a, {2, 2, 2, 2}];
a[[1, ;; , 1, ;;]] == a1[[1 ;; 2, 1 ;; 2]] (*True*)
a[[1, ;; , 2, ;;]] == a1[[1 ;; 2, 3 ;; 4]] (*True*)
a[[2, ;; , 1, ;;]] == a1[[3 ;; 4, 1 ;; 2]] (*True*)
a[[2, ;; , 2, ;;]] == a1[[3 ;; 4, 3 ;; 4]] (*True*)


This is exactly like the python (unlike Mathematica, in python array indexing starts from 0) snippet mentioned in the question.

The resulting tensor is of the type $$a_{k,l}^{i,j}$$ where each of the index runs from $$1 \to 2$$, in indexing language, it is a[[i, j , k, l]]

MatrixForm of a high order tensor has itself logic: For even order tensors, it express tensors as nested matrices, with indices starting from outside to inside and from row to column, and for odd order tensors, except expressing the last dimension as a vector, the remaining rules outside is the same as even order case. Probably it is not the same as our intuition. To understand ArrayReshape, it is better to use ArrayRules for checking. In your example, it is:

ArrayRules[ArrayReshape[Range[16], {2, 2, 2, 2}]]
{{1, 1, 1, 1} -> 1, {1, 1, 1, 2} -> 2, {1, 1, 2, 1} ->
3, {1, 1, 2, 2} -> 4, {1, 2, 1, 1} -> 5, {1, 2, 1, 2} ->
6, {1, 2, 2, 1} -> 7, {1, 2, 2, 2} -> 8, {2, 1, 1, 1} ->
9, {2, 1, 1, 2} -> 10, {2, 1, 2, 1} -> 11, {2, 1, 2, 2} ->
12, {2, 2, 1, 1} -> 13, {2, 2, 1, 2} -> 14, {2, 2, 2, 1} ->
15, {2, 2, 2, 2} -> 16, {_, _, _, _} -> 0}


You can see that the array Range[16] is reshaped by dimension correctly and strictly.

• ArrayReshape[Range[16], {2, 2, 2, 2}] yields {{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}, {{{9, 10}, {11, 12}}, {{13, 14}, {15, 16}}}} which is not the result that the OP states that he wants. Oct 26, 2022 at 23:27
• The user expressed that the MatrixForm of reshaped array is not one wanted. However I don't know what is the user's real purpose for result. Probably OP just doesn't agree with MatrixForm results. It's better for me to know his real needed tensor by element specifications. Oct 27, 2022 at 12:52