Message Boards Message Boards

0
|
8528 Views
|
6 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Flattening an array of blocks into a single matrix?

Posted 8 years ago

Hello

I read in the MMA help for Flatten that
"If the mij are matrices, Flatten[{{m11,m12},{m21,m22}},{{1,3},{2,4}}] effectively constructs a single matrix from the "blocks" mij. "

This line is too me too terse to me as I cannot understand from the parameters {{1,3},{2,4}} the logic behind the construction of that single matrix (what do these parameters correspond to ?) and when you modify the order of these parameters the result is quite puzzlling. .

Let's take an example.

u = {{a, b}, {c, d}}
exp = {{0 u, u}, {2 u, 3 u}} 
Flatten[exp, {{1, 4}, {2, 3}}] // MatrixForm (* Example similar to the one from the Help *)
Flatten[exp, {{3, 4}, {2, 1}}] // MatrixForm (* How do you explain  the reordering you get in the matrix from these parameters ? *)
Flatten[exp, {{4, 2}, {1, 3}}] // MatrixForm (*  same question *)

Moreover if the initial blocks are not of the same shape, you may get a single matrix from a given order of the parameters between the curly braces:

  u = {{a, b}, {c, d, e}}
  exp = {{0 u, u}, {2 u, 3 u}} 
  Flatten[exp, {{1, 4}, {2, 3}}] // MatrixForm     (*the result is not a single matrix *)
  Flatten[exp, {{3, 4}, {2, 1}}] // MatrixForm      (* here it is *)

How do you explain the last example?

Thanks

POSTED BY: Jan Potocki
6 Replies

Did you miss something important from the help?

Flatten[list] flattens out nested lists

In your examples, to obtain nested lists you must add one pair of curly brackets { }. So:

In[31]:= u = {{a, b}, {c, d}}
exp = {{0 u, u}, {2 u, 3 u}}
Flatten[{exp, {{1, 4}, {2, 
    3}}}](*Example similar to the one from the Help*)
Flatten[{{exp, {{3, 4}, {2, 
     1}}}}](*How do you explain the reordering you get in the matrix \
from these parameters?*)
Flatten[{exp, {{4, 2}, {1, 3}}}] (*same question*)

Out[31]= {{a, b}, {c, d}}

Out[32]= {{{{0, 0}, {0, 0}}, {{a, b}, {c, d}}}, {{{2 a, 2 b}, {2 c, 
    2 d}}, {{3 a, 3 b}, {3 c, 3 d}}}}

Out[33]= {0, 0, 0, 0, a, b, c, d, 2 a, 2 b, 2 c, 2 d, 3 a, 3 b, 3 c, 
 3 d, 1, 4, 2, 3}

Out[34]= {0, 0, 0, 0, a, b, c, d, 2 a, 2 b, 2 c, 2 d, 3 a, 3 b, 3 c, 
 3 d, 3, 4, 2, 1}

Out[35]= {0, 0, 0, 0, a, b, c, d, 2 a, 2 b, 2 c, 2 d, 3 a, 3 b, 3 c, 
 3 d, 4, 2, 1, 3}

Incorrect, the second argument:

 {{1, 4}, {2, 3}}

is the level-specification! not the matrix!

POSTED BY: Sander Huisman

You also might be interested in ArrayFlatten

http://reference.wolfram.com/language/ref/ArrayFlatten.html

POSTED BY: Sam Carrettie

Nice find! though this can only do it in the 'standard way'.

POSTED BY: Sander Huisman
Posted 8 years ago

Thanks, it is quite difficult to grasp and even more to remember.You are not helped by Trace either as it doesn't show what's going on step by step.

POSTED BY: Jan Potocki

Basically the list of lists as the second argument of Flatten are a list of indices that are combined to form a single dimensions.

u = {{a, b}, {c, d}}
exp = {{0 u, u}, {2 u, 3 u}} 
Flatten[exp, {{1, 4}, {2, 3}}] // MatrixForm (* Example similar to the one from the Help *)
Flatten[exp, {{3, 4}, {2, 1}}] // MatrixForm (* How do you explain  the reordering you get in the matrix from these parameters ? *)
Flatten[exp, {{4, 2}, {1, 3}}] // MatrixForm (*  same question *)

the second Flatten combines dimensions 3 and 4, that is to say it flattens the deepest dimensions to get something like:

enter image description here

Then you combine the two first dimensions in two ways:

enter image description here

To get:

enter image description here

Regarding your last example, these are 'matrices' but not rectangular ones (but ragged) because your input is not rectangular. You can visualize them using Grid rather than MatrixForm:

u = {{a, b}, {c, d, e}}
exp = {{0 u, u}, {2 u, 3 u}}
Flatten[exp, {{1, 4}, {2, 3}}] // Grid        
Flatten[exp, {{3, 4}, {2, 1}}] // Grid   

Both have two dimensions but only one is rectangular...

Just as a side note; I've been using Mathematica now for 12-13 years or so, but these indices still don't come naturally to me, I still have to think hard...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract