Re: Matrix construction
- To: mathgroup at smc.vnet.net
- Subject: [mg101120] Re: [mg101059] Matrix construction
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Wed, 24 Jun 2009 06:35:06 -0400 (EDT)
- References: <200906231103.HAA08024@smc.vnet.net>
Hi,
joining, for instance,
{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}, {{9, 10}, {11, 12}}, {{13,
14}, {15, 16}}}
we have to get
{{1, 2, 5, 6}, {3, 4, 7, 8}, {9, 10, 13, 14}, {11, 12, 15, 16}}
A possible solution:
In[1] =
joinBlocks[matrixOfMatrices_?(MatrixQ[#,MatrixQ]&)] :=
Flatten[Map[ Join @@@ Transpose[#] &, matrixOfMatrices], 1];
For example,
{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}}//MatrixForm
(output suppressed),
In[2]=
joinBlocks[{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}}]
Out[2]=
{{1,2,5,6},{3,4,7,8},{9,10,13,14},{11,12,15,16}}
This works on more general matrices as well. For example, on
Fold[Partition, Range[48], {2, 2, 3}] // MatrixForm
(output suppressed)
In[3] = joinBlocks[Fold[Partition, Range[48], {2, 2, 3}]]
Out[3] = {{1, 2, 5, 6, 9, 10}, {3, 4, 7, 8, 11, 12}, {13, 14, 17, 18, 21,
22}, {15, 16, 19, 20, 23, 24}, {25, 26, 29, 30, 33, 34}, {27, 28,
31, 32, 35, 36}, {37, 38, 41, 42, 45, 46}, {39, 40, 43, 44, 47, 48}}
Hope this helps.
Regards,
Leonid
On Tue, Jun 23, 2009 at 4:03 AM, Haibo Min <yshdfeng at gmail.com> wrote:
> Hi, everyone.
> I know that matrix can be constructed via Table or Array, but I want to
> construct a matrix from other matrices, how to do it?
> For example, suppose I would like to construct a matrix M which is composed
> of four matrices, A,B,C,D. The element corresponding to the first row and
> the first column is A, the first row and second column is B...
> I used the M={{A,B},{C,D}} and M={A,B,C,D}, but both of them didn't work.
> How?
> Thanks for advance.
>
> Best Regards,
> Haibo
>
>
>
- References:
- Matrix construction
- From: Haibo Min <yshdfeng@gmail.com>
- Matrix construction