MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Flattening a hypermatrix into an ordinary matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126733] Re: Flattening a hypermatrix into an ordinary matrix
  • From: Andrzej Kozlowski <akozlowski at gmail.com>
  • Date: Sat, 2 Jun 2012 05:48:24 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201206010919.FAA11851@smc.vnet.net>



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

ArrayFlatten[A] is exactly for this purpose.

Andrzej Kozlowski



  • Prev by Date: Re: Flattening a hypermatrix into an ordinary matrix
  • Next by Date: Re: Flattening a hypermatrix into an ordinary matrix
  • Previous by thread: Re: Flattening a hypermatrix into an ordinary matrix
  • Next by thread: Re: Flattening a hypermatrix into an ordinary matrix