RE: making a block diagonal matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg37580] RE: [mg37548] making a block diagonal matrix
- From: "David Park" <djmp at earthlink.net>
- Date: Tue, 5 Nov 2002 05:02:50 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
David,
Needs["LinearAlgebra`MatrixManipulation`"]
mat1 = {{a, b}, {c, d}};
mat2 = {{e, f, g}, {h, i, j}, {k, l, m}};
BlockMatrix[{{mat1, ZeroMatrix[2, 3]}, {ZeroMatrix[3, 2],
mat2}}] // MatrixForm
MatrixManipulation doesn't have a routine to make a diagonal block matrix
directly, but I think the following will work.
diagonalBlockMatrix[blocks : {__?(MatrixQ[#] && Equal @@ Dimensions[#] &)}]
:=
Module[
{n = Length[blocks], sizes, rows, cols, i, j},
sizes = Dimensions /@ blocks;
rows = Flatten@(First /@ sizes);
cols = Flatten@(Last /@ sizes);
Table[
Join[Table[
ZeroMatrix[Part[rows, i], Part[cols, j]], {j, 1, i - 1}], {Part[
blocks, i]},
Table[ZeroMatrix[Part[rows, i], Part[cols, j]], {j, i + 1, n}]],
{i,
1, n}] // BlockMatrix
]
diagonalBlockMatrix[{mat1, mat2}] // MatrixForm
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: David E. Burmaster [mailto:deb at alceon.com]
To: mathgroup 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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++