COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Flattening a hypermatrix into an ordinary matrix

• Follow

```I have an n x n hypermatrix, the entries of which are m x m blocks.
For example A below (m=3, n=2):

A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
A={{ A11,A12},{A21,A22}};

I want to convert this to an ordinary n*m x n*m matrix.
For the example I want A to become

{{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
{B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
{D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}

This can be easily done with C style loops as

AA=Table[0,{m*n},{m*n}];
For [i=1,i<=n,i++, For[j=1,j<=n,j++,
For [k=1,k<=m,k++, For [l=1,l<=m,l++,
AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
]]]];

but is there a more elegant way using Flatten?
(Flatten[A,1] doesnt do it.)  It should work also for blocks
of varying size for future use.

```
 0
Reply carlos151 (330) 6/1/2012 9:21:40 AM

```On Friday, June 1, 2012 4:21:40 AM UTC-5, car...@colorado.edu wrote:
> I have an n x n hypermatrix, the entries of which are m x m blocks.
> For example A below (m=3, n=2):
>
> A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
> A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
> A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
> A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
> A={{ A11,A12},{A21,A22}};
>
> I want to convert this to an ordinary n*m x n*m matrix.
> For the example I want A to become
>
>  {{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
>   {B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
>   {D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}
>
> This can be easily done with C style loops as
>
> AA=Table[0,{m*n},{m*n}];
> For [i=1,i<=n,i++, For[j=1,j<=n,j++,
>     For [k=1,k<=m,k++, For [l=1,l<=m,l++,
>          AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
>          ]]]];
>
> but is there a more elegant way using Flatten?
> (Flatten[A,1] doesnt do it.)  It should work also for blocks
> of varying size for future use.

In[449]:= ArrayFlatten[A]

Out[449]= {{B11, B12, B13, C11, C12, C13}, {B21, B22, B23, C21, C22,
C23}, {B31, B32, B33, C31, C32, C33}, {D11, D12, D13, E11, E12,
E13}, {D21, D22, D23, E21, E22, E23}, {D31, D32, D33, E31, E32,
E33}}

(I was actually surprised to learn that ArrayFlatten did this "out of the box", so to speak. But then, I'm easily surprised.)

Daniel Lichtblau
Wolfram Research

```
 0
Reply danl1 (927) 6/2/2012 9:45:14 AM

```On Jun 1, 2:21 am, car...@colorado.edu wrote:
> I have an n x n hypermatrix, the entries of which are m x m blocks.
> For example A below (m=3, n=2):
>
> A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
> A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
> A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
> A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
> A={{ A11,A12},{A21,A22}};
>
> I want to convert this to an ordinary n*m x n*m matrix.
> For the example I want A to become
>
>  {{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
>   {B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
>   {D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}
>
> This can be easily done with C style loops as
>
> AA=Table[0,{m*n},{m*n}];
> For [i=1,i<=n,i++, For[j=1,j<=n,j++,
>     For [k=1,k<=m,k++, For [l=1,l<=m,l++,
>          AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
>          ]]]];
>
> but is there a more elegant way using Flatten?
> (Flatten[A,1] doesnt do it.)  It should work also for blocks
> of varying size for future use.

Flatten /@ Flatten[Transpose[A,{1,3,2,4}],1]

will also do it and is a little cleaner than my previous suggestion.

```
 0
Reply koopman (753) 6/2/2012 9:45:44 AM

```On 6/1/2012 4:21 AM, carlos@colorado.edu wrote:
> I have an n x n hypermatrix, the entries of which are m x m blocks.
> For example A below (m=3, n=2):
>
> A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
> A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
> A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
> A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
> A={{ A11,A12},{A21,A22}};
>
> I want to convert this to an ordinary n*m x n*m matrix.
> For the example I want A to become
>
>   {{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
>    {B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
>    {D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}
>
> This can be easily done with C style loops as
>
> AA=Table[0,{m*n},{m*n}];
> For [i=1,i<=n,i++, For[j=1,j<=n,j++,
>      For [k=1,k<=m,k++, For [l=1,l<=m,l++,
>           AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
>           ]]]];
>
> but is there a more elegant way using Flatten?
> (Flatten[A,1] doesnt do it.)  It should work also for blocks
> of varying size for future use.
>

May be a Flatten/Table/Join combination?

-----------------------
m=3;n=2;
Flatten[Table[Join[A[[j,1,i]],A[[j,2,i]]],{j,1,n},{i,1,m}],1]
---------------------------

{{B11, B12, B13, C11, C12, C13}, {B21, B22, B23, C21, C22, C23},
{B31,B32, B33, C31, C32, C33}, {D11, D12, D13, E11, E12, E13},
{D21, D22, D23, E21, E22, E23}, {D31, D32, D33, E31, E32, E33}}

--Nasser

```
 0

```On Jun 1, 2:21 am, car...@colorado.edu wrote:
> I have an n x n hypermatrix, the entries of which are m x m blocks.
> For example A below (m=3, n=2):
>
> A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
> A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
> A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
> A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
> A={{ A11,A12},{A21,A22}};
>
> I want to convert this to an ordinary n*m x n*m matrix.
> For the example I want A to become
>
>  {{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
>   {B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
>   {D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}
>
> This can be easily done with C style loops as
>
> AA=Table[0,{m*n},{m*n}];
> For [i=1,i<=n,i++, For[j=1,j<=n,j++,
>     For [k=1,k<=m,k++, For [l=1,l<=m,l++,
>          AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
>          ]]]];
>
> but is there a more elegant way using Flatten?
> (Flatten[A,1] doesnt do it.)  It should work also for blocks
> of varying size for future use.

In[1]:=
A11 = {{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
A12 = {{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
A21 = {{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
A22 = {{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
A   = {{ A11,A12},{A21,A22}};

In[6]:=
Flatten[Map[Flatten,Transpose[A,{1,3,2,4}],{2}],1]

Out[6]=
{{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
{B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
{D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}

```
 0
Reply koopman (753) 6/2/2012 9:49:49 AM

```On Fri, 01 Jun 2012 10:21:40 +0100, <carlos@colorado.edu> wrote:

> I have an n x n hypermatrix, the entries of which are m x m blocks.
> For example A below (m=3, n=2):
>
> A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
> A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
> A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
> A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
> A={{ A11,A12},{A21,A22}};
>
> I want to convert this to an ordinary n*m x n*m matrix.
> For the example I want A to become
>
>  {{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
>   {B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
>   {D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}
>
> This can be easily done with C style loops as
>
> AA=Table[0,{m*n},{m*n}];
> For [i=1,i<=n,i++, For[j=1,j<=n,j++,
>     For [k=1,k<=m,k++, For [l=1,l<=m,l++,
>          AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
>          ]]]];
>
> but is there a more elegant way using Flatten?
> (Flatten[A,1] doesnt do it.)  It should work also for blocks
> of varying size for future use.
>

Yes; you can use:

Flatten[A, {{1, 3}, {2, 4}}]

{{B11, B12, B13, C11, C12, C13}, {B21, B22, B23, C21, C22, C23},
{B31, B32, B33, C31, C32, C33}, {D11, D12, D13, E11, E12, E13},
{D21, D22, D23, E21, E22, E23}, {D31, D32, D33, E31, E32, E33}}

which flattens together levels 1 and 3 to form level 1 in the result, and
levels 2 and 4 to form level 2. (I often find it helpful to look at
Dimensions[A], which is {2, 2, 3, 3}, to figure out what is needed in
cases like this.)

Since the Flatten documentation is rather terse, for a more detailed
description of how this works, I'd suggest having a look at Leonid

http://mathematica.stackexchange.com/questions/119/flatten-command-matrix-as-second-argument

```
 0

```On 2012.06.01. 11:21, carlos@colorado.edu wrote:
> I have an n x n hypermatrix, the entries of which are m x m blocks.
> For example A below (m=3, n=2):
>
> A11={{B11,B12,B13},{B21,B22,B23},{B31,B32,B33}};
> A12={{C11,C12,C13},{C21,C22,C23},{C31,C32,C33}};
> A21={{D11,D12,D13},{D21,D22,D23},{D31,D32,D33}};
> A22={{E11,E12,E13},{E21,E22,E23},{E31,E32,E33}};
> A={{ A11,A12},{A21,A22}};
>
> I want to convert this to an ordinary n*m x n*m matrix.
> For the example I want A to become
>
>   {{B11,B12,B13,C11,C12,C13},{B21,B22,B23,C21,C22,C23},
>    {B31,B32,B33,C31,C32,C33},{D11,D12,D13,E11,E12,E13},
>    {D21,D22,D23,E21,E22,E23},{D31,D32,D33,E31,E32,E33}}
>
> This can be easily done with C style loops as
>
> AA=Table[0,{m*n},{m*n}];
> For [i=1,i<=n,i++, For[j=1,j<=n,j++,
>      For [k=1,k<=m,k++, For [l=1,l<=m,l++,
>           AA[[m*(i-1)+k,m*(j-1)+l]]=A[[i,j,k,l]]
>           ]]]];
>
> but is there a more elegant way using Flatten?
> (Flatten[A,1] doesnt do it.)  It should work also for blocks
> of varying size for future use.
>

You're looking for ArrayFlatten[A].

For more complex uses of Flatten, you might enjoy reading this guide:

http://mathematica.stackexchange.com/questions/119/flatten-command-matrix-as-second-argument

--
Szabolcs Horv�t
Visit Mathematica.SE:  http://mathematica.stackexchange.com/

```
 0
Reply szhorvat (1430) 6/2/2012 9:51:52 AM

6 Replies
44 Views

Similiar Articles:

7/20/2012 9:49:19 AM