Re: making a block diagonal matrix (2)
- To: mathgroup at smc.vnet.net
- Subject: [mg37605] Re: making a block diagonal matrix (2)
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 6 Nov 2002 06:54:06 -0500 (EST)
- References: <aq598m$sdj$1@smc.vnet.net> <aq86eu$e4f$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I tidy up my previous posting and make it applicable to rectangular blocks:
BlockDiagonalMatrix[mats_] :=
Module[{pl, dms, tln},
pl = 0;
tln = Plus @@ (Last[Dimensions[#]] & /@ mats);
Join @@ (PadRight[#, {First[dms = Dimensions[#]], tln},
0, {0, First[{pl, pl += dms[[2]]}]}] & /@ mats)
]
Test,
BlockDiagonalMatrix[{{{1, 1}, {1, 1}}, {{2, 2}}, {{3}, {3}}}] // TableForm
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Allan Hayes" <hay at haystack.demon.co.uk> wrote in message
news:aq86eu$e4f$1 at smc.vnet.net...
> David,
>
> DiagonalMatrixSq[sms_]:=
> Module[{p=0},
> Join@@(PadRight[#,{Length[#],Plus@@Length/@sms},
> 0,{0,(p+=Length[#])-Length[#]}]&/@sms)
> ]
>
> Test:
>
> sms= {{{a,b},{c,d}},{{e,f,g},{h,i,j},{k,l,m}},{{z}}};
>
> DiagonalMatrixSq[sms]
>
> {{a,b,0,0,0,0},
> {c,d,0,0,0,0},
> {0,0,e,f,g,0},
> {0,0,h,i,j, 0},
> {0,0,k,l,m,0},
> {0,0,0,0,0,z}}
>
> --
> Allan
>
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
>
>
> "David E. Burmaster" <deb at alceon.com> wrote in message
> news:aq598m$sdj$1 at smc.vnet.net...
> > Dear MathGroup
> >
> > Can anyone please suggest an efficient way to convert a list of square
> > matrices (of different dimensions) into a block diagonal matrix?? The
> > elements of each matrix are all real numbers
> >
> > here is an example with a list of two square matrices --
> >
> > matrix1 = a b
> > c d
> >
> > matrix2 = e f g
> > h i j
> > k l m
> >
> >
> > output = a b 0 0 0
> > c d 0 0 0
> > 0 0 e f g
> > 0 0 h i j
> > 0 0 k l m
> >
> >
> >
> > =-=
> >
> > many thanks
> > dave
> >
> >
> >
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> > David E. Burmaster, Ph.D.
> > Alceon Corporation
> > POBox 382069 (new Box number effective 1 Sep 2001)
> > Harvard Square Station
> > Cambridge, MA 02238-2069 (new ZIP code effective 1 Sep 2001)
> >
> > Voice 617-864-4300
> >
> > Web http://www.Alceon.com
> > Email deb at Alceon.com
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >
> >
> >
>
>
>