MATLAB: Matrix column subtraction problem

columnmatrixsubtraction

I know this is really basic stuff but unfortunately I can't get to the right solution.
I have for example these 2 matrices:
matrixA=[1 5;5 2;0 0;3 2;3 6]
and
matrixB=[4 5 6;8 5 2;3 3 2;1 2 1;2 5 4]
What I want to do is to subtract all columns in matrixB from column 1 and column 2 respectively in matrixA. So I want to make
matrixC=[1-4 1-5 1-6 5-4 5-5 5-6;5-8 5-5 5-2 2-8 2-5 2-2;....;....;.....]

Best Answer

C = zeros(5,2*3);
for p = 1:2
for q = 1:3
C(:,3*p+q-3) = A(:,p)-B(:,q);
end
end
Or this:
C = reshape(repmat(reshape(A,5,1,2),1,3,1)-repmat(reshape(B,5,3,1),1,1,2),5,[]);