Re: making a block diagonal matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg37568] Re: [mg37548] making a block diagonal matrix
- From: BobHanlon at aol.com
- Date: Tue, 5 Nov 2002 05:00:36 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 11/4/2002 4:33:42 AM, deb at alceon.com writes: >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 > matrix1 = {{a, b}, {c, d}}; matrix2 = {{e, f, g}, {h, i, j}, {k, l, m}}; matrix3 = {{n, o}, {p, q}}; Fold[ Module[{len = Length[#1] + Length[#2]}, Join[ PadRight[#, len] & /@ #1, PadLeft[#, len] & /@ #2]] &, {}, {matrix1, matrix2, matrix3}] {{a, b, 0, 0, 0, 0, 0}, {c, d, 0, 0, 0, 0, 0}, {0, 0, e, f, g, 0, 0}, {0, 0, h, i, j, 0, 0}, {0, 0, k, l, m, 0, 0}, {0, 0, 0, 0, 0, n, o}, {0, 0, 0, 0, 0, p, q}} Bob Hanlon