# combination of two matrices

1 view (last 30 days)
Najiya Omar on 12 Dec 2016
Commented: James Tursa on 13 Dec 2016
I have two matrices (160,12) in size. I want to put them in one matrix (320,12)by making the first 40 rows in matrix 1 following by the first 40 rwos in matrix 2, creating the first 80 rows of the new matrix. the second 40 rows in matrix 1 following by the second 40 rwos in matrix 2, creating the second 80 rows and so on.

James Tursa on 13 Dec 2016
A = 160 x 12 matrix
B = 160 x 12 matrix
Ar = reshape(A',40*12,[]);
Br = reshape(B',40*12,[]);
result = reshape([Ar;Br],12,[])';
Najiya Omar on 13 Dec 2016
Amazing!! thank you James so so much

John BG on 13 Dec 2016
There is not need to reshape
[szA1 szA2]=size(A);[szB1 szB2]=size(B);
[A(:,[1:floor(szA2/2)]) B(:,[1:floor(szA2/2)]) A(:,[floor(szA2/2)+1:end]) B(:,[floor(szA2/2)+1:end])]
James Tursa on 13 Dec 2016
Reshaping a full matrix produces a shared data copy, not a deep data copy. So no elements get moved in memory at all.