MATLAB: Matrix columnwise subtraction

elementssubtraction

I have the following matrix:
[ 1 2 3
1 3 4
4 5 6]
I need:
[ 0 -1 -2
0 -2 -3
0 -1 -2]
How can I do it?

Best Answer

Try bsxfun:
% Generate sample data.
m = rand(3,5)
% Extract out the first column
m1 = m(:,1)
% Subtract first column from all the columns.
mMinusM1 = bsxfun(@minus,m,m1)