Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of Flatten in Mathematica?

What's the inverse of f[x_]:=Flatten[x] where x is Array with dimensions dims?

like image 769
Yaroslav Bulatov Avatar asked Sep 27 '10 21:09

Yaroslav Bulatov


1 Answers

There is no built-in function, but it's pretty easy with a combination of Fold and Partition:

In[47]:= x1 = RandomReal[{0, 1}, {3, 4, 5}];

In[48]:= dims = Dimensions[x1]

Out[48]= {3, 4, 5}

In[49]:= x2 = Fold[Partition, Flatten[x1], Most[Reverse[dims]]];

In[50]:= x1 == x2

Out[50]= True
like image 55
Michael Pilat Avatar answered Nov 02 '22 19:11

Michael Pilat