RE: making a block diagonal matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg37569] RE: [mg37548] making a block diagonal matrix
- From: F.H.Simons at tue.nl
- Date: Tue, 5 Nov 2002 05:00:48 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Dave, One way of doing this task is by using padding. Let us start with a list of square matrices: In[1]:= matrices = Table[Array[#1+#2&, {i,i}], {i, 2, 4}] Out[1]= {{{2,3},{3,4}},{{2,3,4},{3,4,5},{4,5,6}}, {{2,3,4,5},{3,4,5,6},{4,5,6,7},{5,6,7,8}}} So we know the size of the result we are looking for: In[2]:= size = Plus @@ Dimensions /@ matrices Out[2]= {9,9} We construct a list of the matrices together with their position in the result: In[3]:= aux = Transpose[{matrices, Drop[ FoldList[Plus, {0,0}, Dimensions /@ matrices], -1]}] Out[3]= {{{{2,3},{3,4}},{0,0}},{{{2,3,4},{3,4,5},{4,5,6}},{2, 2}},{{{2,3,4,5},{3,4,5,6},{4,5,6,7},{5,6,7,8}},{5,5}}} Then the result can be found by padding in the following way: In[4]:= Fold[ PadRight[ #2[[1]], size, RotateLeft[ #1, #2[[2]]], #2[[2]]]&, Array[0&, size], aux] Out[4]= {{2,3,0,0,0,0,0,0,0},{3,4,0,0,0,0,0,0,0},{0,0,2,3,4,0,0,0,0}, {0,0,3,4,5,0,0,0,0},{0,0,4,5,6,0,0,0,0},{0,0,0,0,0,2,3,4,5}, {0,0,0,0,0,3,4,5,6},{0,0,0,0,0,4,5,6,7},{0,0,0,0,0,5,6,7,8}} Fred Simons Eindhoven University of Technology > -----Original Message----- > From: David E. Burmaster [mailto:deb at alceon.com] To: mathgroup at smc.vnet.net > Sent: maandag 4 november 2002 8:45 > To: mathgroup at smc.vnet.net > Subject: [mg37569] [mg37548] making a block diagonal matrix > > > 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 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > >