MATLAB: Replacing alternate columns of matrix with another matrix

MATLABmatrixmatrix manipulation

I have a matrix A and B, i need to replace alternate rows of B with A to form C
A={
1
2
3
}
B= {
4 7 10
5 8 11
6 9 12
}
C= {
1 4 1 7 1 10
2 5 2 8 2 11
3 6 3 9 3 12
}
Please let me know how to do this

Best Answer

Here is one way:
[m,n] = size(B);
C = zeros(m,2*n);
C(:,1:2:end) = repmat(A,[1,n]);
C(:,2:2:end) = B;