Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
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 '12 at 15:53
3  
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! – Gabriel Oct 16 '12 at 15:57

migrated from stackoverflow.com Oct 16 '12 at 15:41

3 Answers

Just apply transpose to the top answer:

reshape[mtx_, n_, _] := Transpose[Partition[Flatten[mtx], n]];
share|improve this answer

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}}
share|improve this answer
1  
(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.) – J. M. Oct 16 '12 at 16:52
Sorry, vote removed as I forgot this wasn't in v7, and I'm trying to be fair about that. – Mr.Wizard Oct 17 '12 at 12:07
No worries. I was scratching my head at the blinking rep, tho. :) – J. M. Oct 17 '12 at 12:13

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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