Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtraction two vectors in Matlab [duplicate]

Possible Duplicate:
MATLAB: Matrix of differences

I am not sure how to word this question but I will do my best:

I have two vectors, A and B.

I want to subtract the all values in A by every individual value of B.

For example, all values in A are subtracted by the first value of B. Then the all values of A are subtracted by the second value of B, and so on.

The resultant matrix should be length(A) x length(B) and look something like this:

Ans = [A - B(1); A - B(2); A - B(3); ....... ]

Is there any way of doing this without a loop?

like image 571
gtdevel Avatar asked Mar 05 '26 21:03

gtdevel


1 Answers

The line like @Memming and @Jonas says:

Result = bsxfun(@minus, a, b');
like image 79
Barney Szabolcs Avatar answered Mar 08 '26 15:03

Barney Szabolcs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!