MATLAB: How to subtract all the values of one vector, one by one form an other vector, without using a for loop

for loopoptimization

I am trying to optimize a for loop, where i want to remove an inner for loop by looping an entire vector, instead of looping each value of the vector.
I want to subtract all of the values from one vektor V, from an other vector B, so that I get a B(:)-V(n) vector, for each n value of V:
eksampel:
V = [1,2,3]
B = [4;11]
(The dimensions of the output vectors does not matter)
Result = [4-1,11-1;4-2,11-2;4-3,11-3]

Best Answer

The bsxfun function is perfect for this:
V = [1,2,3];
B = [4;11];
Result = bsxfun(@minus, B, V)';