# Reshape vectors exactly like in MATLAB

This gives a 64×1 column vector in Mathematica:

F := Flatten[ArrayFlatten[
ArrayFlatten[
Table[Subscript[f, i, j, k, l, m,
n], {i, 0, 1}, {l, 0, 1}, {j, 0, 1}, {m, 0, 1}, {k, 0, 1}, {n,
0, 1}]]]] // MatrixForm


as F=(1:64)' does in MATLAB.

In MATLAB, reshape(F,16,4) gives a 16×4 matrix, where column 1 is the first 16 elements of F, column 2 the 17th to 32nd, etc.

In Mathematica, the best equivalents for reshaping seem to be the top two answers here.

But when I apply either of these commands, I do not get a 16×4 matrix that's constructed like reshape in MATLAB. Instead I get a 16×4 matrix where row 1 is the first four elements of F, etc.

I have tried adding transpose operations in every location imaginable, and still can't get my Mathematica output to match the MATLAB. I've also tried wrapping List[] around the definition of F to make F appear like a row instead of a column, but everything I do seems to not work.

Any help is much appreciated!

• As dlimpid says, you just need a Transpose to the existing answer. Another related question that might be of interest: mathematica.stackexchange.com/q/10582/5
– rm -rf
Oct 16, 2012 at 15:53
• Just for interest as well to make the equivalent of F=(1:64)' in Mathematica you just do Transpose[{Range[1,64]}]. Also there is no reason to use := (SetDelayed) in your example, you should use just = (Set) as nothing changes from call to call. Good luck! Oct 16, 2012 at 15:57
• That's because MATLAB stores matrix elements in column major order while Mathematica stores them in row major order. "Reshaping" is reinterpreting the stored data without changing it, thus the storage scheme matters Jul 9, 2013 at 8:12

Just apply transpose to the top answer:

reshape[mtx_, n_, _] := Transpose[Partition[Flatten[mtx], n]];


ArrayReshape (new in version 9) does just that.

• One still has to Transpose the result to get the output desired in the OP. Jul 9, 2013 at 5:03
• @MichaelE2 Maybe, I don't have a Matlab copy to try it. My answer addresses the parts of the question dedicated to Mathematica reshaping tools. Jul 9, 2013 at 5:49
• @MatthiasOdisio ArrayReshape does the same operation as MATLAB's reshape (and thus it's an $O(1)$ operation). The reason why the results still differ up to a transposition is that MATLAB stores matrices column-by-column while Mma stores them row-by-row. Jul 9, 2013 at 8:16
• @Szabolcs Thanks, your comment is helpful (not like this one of mine!) Jul 9, 2013 at 23:52

You might also use the (undocumented) function InternalDeflatten[] for the purpose:

reshape[arr_List, dims : {__Integer}] :=
Transpose[InternalDeflatten[Flatten[arr], Reverse[dims]]]

reshape[Range[16*4], {16, 4}]
{{1, 17, 33, 49}, {2, 18, 34, 50}, {3, 19, 35, 51}, {4, 20, 36, 52},
{5, 21, 37, 53}, {6, 22, 38, 54}, {7, 23, 39, 55}, {8, 24, 40, 56},
{9, 25, 41, 57}, {10, 26, 42, 58}, {11, 27, 43, 59}, {12, 28, 44, 60},
{13, 29, 45, 61}, {14, 30, 46, 62}, {15, 31, 47, 63}, {16, 32, 48, 64}}

• (On the other hand, the new dimensions $p\times q$ should be commensurate with the old dimensions $m\times n$ (that is, $pq=mn$); otherwise, the function will crash the kernel.) Oct 16, 2012 at 16:52

Since Deflatten isn't in version 7 here is my proposal:

reshape[a_, d__] := Fold[Partition, a, Reverse@{d}] ~Flatten~ {1, 3}


Which could also be written:

reshape[a_, r___, p_] := reshape[a ~Partition~ p, r]

reshape[a_] := a ~Flatten~ {1, 3}


Test:

reshape[Range@24, 3, 8] // MatrixForm


reshape[Range@24, 3, 4, 2] // MatrixForm


reshape[Range@24, 6, 2, 2] // MatrixForm