Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10329 > unrolled thread

Re: Flattening a hypermatrix into an ordinary matrix

Started byBob Hanlon <hanlonr357@gmail.com>
First post2012-06-02 09:52 +0000
Last post2012-06-03 09:04 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Flattening a hypermatrix into an ordinary matrix Bob Hanlon <hanlonr357@gmail.com> - 2012-06-02 09:52 +0000
    Re: Flattening a hypermatrix into an ordinary matrix carlos@colorado.edu - 2012-06-03 09:04 +0000

#10329 — Re: Flattening a hypermatrix into an ordinary matrix

FromBob Hanlon <hanlonr357@gmail.com>
Date2012-06-02 09:52 +0000
SubjectRe: Flattening a hypermatrix into an ordinary matrix
Message-ID<jqcnpl$19f$1@smc.vnet.net>
Use Join @@@ Flatten[Transpose /@ A, 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}};

m = 3; n = 2;

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]]]]]]

AA == Join @@@ Flatten[Transpose /@ A, 1]

True

Extending to m=3 and n=3

A13 = A11;
A23 = A21;
A31 = {{F11, F12, F13}, {F21, F22, F23}, {F31, F32, F33}};
A32 = {{G11, G12, G13}, {G21, G22, G23}, {G31, G32, G33}};
A33 = A31;
A = {{A11, A12, A13}, {A21, A22, A23}, {A31, A32, A33}};

m = 3; n = 3;

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]]]]]]

AA == Join @@@ Flatten[Transpose /@ A, 1]

True


Bob Hanlon


On Fri, Jun 1, 2012 at 5:19 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.
>

[toc] | [next] | [standalone]


#10339

Fromcarlos@colorado.edu
Date2012-06-03 09:04 +0000
Message-ID<jqf9bk$a6j$1@smc.vnet.net>
In reply to#10329
On Jun 2, 3:52 am, Bob Hanlon <hanlonr...@gmail.com> wrote:
> Use Join @@@ Flatten[Transpose /@ A, 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}};
>
> m = 3; n = 2;
>
> 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]]]]]]
>
> AA == Join @@@ Flatten[Transpose /@ A, 1]
>
> True
>
> Extending to m=3 and n=3
>
> A13 = A11;
> A23 = A21;
> A31 = {{F11, F12, F13}, {F21, F22, F23}, {F31, F32, F33}};
> A32 = {{G11, G12, G13}, {G21, G22, G23}, {G31, G32, G33}};
> A33 = A31;
> A = {{A11, A12, A13}, {A21, A22, A23}, {A31, A32, A33}};
>
> m = 3; n = 3;
>
> 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]]]]]]
>
> AA == Join @@@ Flatten[Transpose /@ A, 1]
>
> True
>
> Bob Hanlon
>
>
>
>
>
>
>
> On Fri, Jun 1, 2012 at 5:19 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.

Thanks! This seems to work fine, even for variable size blocks.  For
example
(this comes from an actual fluid-structure interaction FEM simulation)

A={{{{B11}},{{C11}},{{D11,D12}},{{E11}},{{F11}}},
    {{{G11},{G12}},{{H11},{H12}},{{I11,I12},{I21,I22}},{{J11},{J12}},
{{K11},{K12}}},
    {{{L11}},{{M11}},{{N11,N12}},{{O11}},{{P11}}},
    {{{Q11}},{{R11}},{{S11,S12}},{{T11}},{{U11}}},
    {{{V11}},{{W11}},{{X11,X12}},{{Y11}},{{Z11}}}};

AA = Join @@@ Flatten[Transpose /@ A, 1] ; Print["AA=",AA//
MatrixForm];

I can't use ArrayFlatten since it does not exist in versions of
Mathematica
such as 4 or 5, which some of my off-campus students use (they were
licensed
by their employers long ago)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web