I have a list:
a = {{1, 2, 3, 4, 1, 2, 3, 4}, {5, 6, 7, 8, 5, 6, 7, 8}};
Using Partition
and Map
I get:
Partition[#, 4]& /@ a
{{{1, 2, 3, 4}, {1, 2, 3, 4}}, {{5, 6, 7, 8}, {5, 6, 7, 8}}}
And now, I would like to get a list which looks like:
{{{{1, 2},{3, 4}}, {{1, 2},{3, 4}}}, {{{5, 6},{7, 8}}, {{5, 6},{7, 8}}}}
I tried to play around with Partition
and Map
, but I did not manage to create such a list. Of course there are easy ways to do it using Do
loops and AppendTo
, but I very much need to avoid Do
loops. Does anybody see the way to create the list?
Answer
There is a new function in Mathematica 9 - ArrayReshape
:
ArrayReshape[a, {2, 2, 2, 2}]
{{{{1, 2}, {3, 4}}, {{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{5, 6}, {7, 8}}}}
Comments
Post a Comment