14
$\begingroup$

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!

$\endgroup$
3
  • $\begingroup$ 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 $\endgroup$
    – rm -rf
    Oct 16, 2012 at 15:53
  • 3
    $\begingroup$ 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! $\endgroup$
    – Gabriel
    Oct 16, 2012 at 15:57
  • $\begingroup$ 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 $\endgroup$
    – Szabolcs
    Jul 9, 2013 at 8:12

4 Answers 4

15
$\begingroup$

Just apply transpose to the top answer:

reshape[mtx_, n_, _] := Transpose[Partition[Flatten[mtx], n]];
$\endgroup$
7
$\begingroup$

ArrayReshape (new in version 9) does just that.

$\endgroup$
4
  • $\begingroup$ One still has to Transpose the result to get the output desired in the OP. $\endgroup$
    – Michael E2
    Jul 9, 2013 at 5:03
  • $\begingroup$ @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. $\endgroup$ Jul 9, 2013 at 5:49
  • 3
    $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Jul 9, 2013 at 8:16
  • $\begingroup$ @Szabolcs Thanks, your comment is helpful (not like this one of mine!) $\endgroup$ Jul 9, 2013 at 23:52
4
$\begingroup$

You might also use the (undocumented) function Internal`Deflatten[] for the purpose:

reshape[arr_List, dims : {__Integer}] :=
   Transpose[Internal`Deflatten[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}}
$\endgroup$
1
  • 3
    $\begingroup$ (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.) $\endgroup$ Oct 16, 2012 at 16:52
4
$\begingroup$

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.