MATLAB: Combine two matrices (every other column)

columnevery otherinterleave

I have two matrices A and B which are for example
A=[1 2 3;4 5 6; 7 8 9] and B=[10 11 12; 13 14 15; 16 17 18]
And I would like to combine these matrices so that every other column is from A and every other is from B. So the answer should be matrix:
[1 10 2 11 3 12; 4 13 5 14 6 15; 7 16 8 17 9 18]
Of course my matrices are not only 3×3 matrices but n x n matrices.
My history with Matlab is so short that I don't figure out if that is even possible to do?

Best Answer

Provided that A and B have the same number of rows and columns,
AB = reshape([A;B], size(A,1), []);
Related Question