MATLAB: Concatenating the several matrices in a certain manner

matrix

Hello all,
I have N J matrices all with the same size. I would like to concatenate them in a way such that I would get a new matrix C with its first J columns be the first row of the J matrices, the second J columns of C consist of second column of all the J matrices and so on. For instance suppose J=2 and I have the following two matrices: s1=[0 0 1 1;1 1 0 0;1 1 0 0;0 0 1 1]; s2=[0 0 -1 -1;-1 -1 0 0;-1 -1 0 0;0 0 -1 -1]; I would like to have C=[0 0 0 0 1 -1 1 -1;1 -1 1 -1 0 0 0 0;1 -1 1 -1 0 0 0 0;0 0 0 0 1 -1 1 -1]; I would appreciate any help!

Best Answer

>> s1=[0 0 1 1;1 1 0 0;1 1 0 0;0 0 1 1]
s1 =
0 0 1 1
1 1 0 0
1 1 0 0
0 0 1 1
>> s2=[0 0 -1 -1;-1 -1 0 0;-1 -1 0 0;0 0 -1 -1]
s2 =
0 0 -1 -1
-1 -1 0 0
-1 -1 0 0
0 0 -1 -1
>> C = [];
>> C(:,2:2:8) = s1;
>> C(:,1:2:8) = s2
C =
0 0 0 0 -1 1 -1 1
-1 1 -1 1 0 0 0 0
-1 1 -1 1 0 0 0 0
0 0 0 0 -1 1 -1 1