MATLAB: Put 3 columns of two matrix in 1 row matrix

column matrixreshaperow

I have matrix X and Y with dimension of 100 x 3
X= [ x1 x2 x3; x4 x5 x6; etc];
Y=[ y1 y2 y3; y4 y5 y6; etc];
Now I want a matrix that combines X and Y with the format as following
XY= [ x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6, etc]
I have tried XY=reshape([ X, Y].',1,[]); but it gave me XY=[ x1 x2 x3 y1 y2 y3 ]
Please helps

Best Answer

X=[1 2 3;4 5 6];
Y=[11 22 33;44 55 66]
X=X';
Y=Y';
a=[X(:) Y(:)]'
out=a(:)'