Re: making a block diagonal matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg37567] Re: making a block diagonal matrix
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Tue, 5 Nov 2002 05:00:27 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <aq598m$sdj$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi, BlockDiagonal[m_?MatrixQ] := m BlockDiagonal[m1_?MatrixQ, m2_?MatrixQ, morems___] := Module[{n1, n2}, n1 = Length[First[m1]]; n2 = Length[First[m2]]; BlockDiagonal[Join[ PadRight[#, n1 + n2, 0] & /@ m1, PadLeft[#, n1 + n2, 0] & /@ m2], morems] ] and BlockDiagonal[ {{a, b}, {d, e}}, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{q, r}, {u, v}}] will work as expected. Regards Jens "David E. Burmaster" wrote: > > 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 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++