Re: *Matrix witihn matrix*
- To: mathgroup at yoda.physics.unc.edu (Roger M. Jones)
- Subject: Re: *Matrix witihn matrix*
- From: Edmund Greaves <egreaves>
- Date: Tue, 28 Jun 94 10:44:12 CDT
>
> I have a matrix containing sub-matrices which I wish to combine.
>
> I have implemented this as:
>
> CatMatrix[a_,b_,c_,d_]:=Block[{t1},
> t1=Length[a];Join[Table[Flatten[{a[[i]],b[[i]]}],
> {i,1,t1}],Table[Flatten[{c[[i]],d[[i]]}],
> {i,1,t1}]]].
>
> So that:
>
> a={{a11,a12,a13},{a21,a22,a23},{a31,a32,a33}};
> b={{b11,b12,b13},{b21,b22,b23},{b31,b32,b33}};
> c={{c11,c12,c13},{c21,c22,c23},{c31,c32,c33}};
> d={{d11,d12,d13},{d21,d22,d23},{d31,d32,d33}};
>
> {a,b,c,d}----->
>
> {{a11, a12, a13, b11, b12, b13}, {a21, a22, a23, b21, b22, b23},
>
>
>
> {a31, a32, a33, b31, b32, b33},
>
>
> {c11, c12, c13, d11, d12, d13},
>
>
>
> {c21, c22, c23, d21, d22, d23}, {c31, c32, c33, d31, d32, d33}}
>
> Does anyone know of a simplier implementation?
>
>
Yes, there is a function in the Mathematica package
LinearAlgebgra`MatrixManipulation` which will perform this operation.
This function is called BlockMatrix and it is used in the following way:
In[23]:=
Needs["LinearAlgebra`MatrixManipulation`"]
In[24]:=
a = Table[ToExpression["a" <> ToString[i] <> ToString[j]],
{i,3}, {j,3}];
In[25]:=
b = Table[ToExpression["b" <> ToString[i] <> ToString[j]],
{i,3}, {j,3}];
In[26]:=
c = Table[ToExpression["c" <> ToString[i] <> ToString[j]],
{i,3}, {j,3}];
In[27]:=
d = Table[ToExpression["d" <> ToString[i] <> ToString[j]],
{i,3}, {j,3}];
In[28]:=
BlockMatrix[{{a,b}, {c,d}}]
Out[28]=
{{a11, a12, a13, b11, b12, b13},
{a21, a22, a23, b21, b22, b23},
{a31, a32, a33, b31, b32, b33},
{c11, c12, c13, d11, d12, d13},
{c21, c22, c23, d21, d22, d23},
{c31, c32, c33, d31, d32, d33}}
In[29]:=
??BlockMatrix
BlockMatrix[block] returns a matrix composed of a matrix of
matrices.
BlockMatrix[LinearAlgebra`MatrixManipulation`Private`block
_] := Apply[AppendColumns,
Apply[AppendRows, LinearAlgebra`MatrixManipulation`Private\
`block, {1}]]
---------------------------------------------------------------------------
Edmund Greaves egreaves at wri.com
{My views may or may not be the views of Wolfram Research}
---------------------------------------------------------------------------