I want to build a 6 by 3 matrix, from the following: one vector is 3 by 1, then a matrix, which is 3 by 2. These will go to the top 3 rows. Now the bottom 3 rows, I'll have 3 vectors each is 3 by 1. Like this:
It is easy to do this, when all the objects are the same. Say we have 6 vectors, each is 3 by 1. Like this:
But because I have a 3 by 2 matrix in there, ArrayFlatten
is not happy for some reason.
I'll show the case with the 6 vectors first.
v1 = {1, 2, 3}; v2 = {4, 5, 6}; v3 = {7, 8, 9}; v4 = {10, 11, 12};
v5 = {13, 14, 15}; v6 = {16, 17, 18};
m = ArrayFlatten[{
{Transpose@{v1}, Transpose@{v2}, Transpose@{v3}},
{Transpose@{v4}, Transpose@{v5}, Transpose@{v6}}
}
];
MatrixForm[m]
Now, in place of v2
and v3
above, I want to put a matrix, of same size as both, which is 3 by 2. Hence:
v1 = {1, 2, 3}; vNew = {{4, 5}, {6, 7}, {8, 9}};
v4 = {10, 11, 12}; v5 = {13, 14, 15}; v6 = {16, 17, 18};
Where vNew
now takes place of v2
and v3
. But when I write
m = ArrayFlatten[{
{Transpose@{v1}, vNew},
{Transpose@{v4}, Transpose@{v5}, Transpose@{v6}}
}
];
It does not work. I tried
m = ArrayFlatten[{
{Join[Transpose@{v1}, vNew, 2]},
{Transpose@{v4}, Transpose@{v5}, Transpose@{v6}}
}
];
And few others things. It only works when I have each element of same shape. So this works
m = ArrayFlatten[{
{Transpose@{v1}, Transpose@{vNew[[All, 1]]}, Transpose@{vNew[[All, 2]]}},
{Transpose@{v4}, Transpose@{v5}, Transpose@{v6}}}];
What should the correct syntax be?
PS: In MATLAB, this is easy:
v1=[1,2,3]'; vNew=[4,5;6,7;8,9];v3=[10,11,12]'; v4=[13,14,15]';v5=[16,17,18]';
m=[v1 vNew;v3 v4 v5]
m =
1 4 5
2 6 7
3 8 9
10 13 16
11 14 17
12 15 18
I am using version 10.0.2.0
{ {v1, v2, v3}, {V4v5v6} } // myArrayFlatten
$\endgroup$