问题
Let's take two vectors:
a = [1 ; 2; 3]
b = [0 ; 9 ; -5]
If I want minimum value of the vector and it's position I can simply:
[x, ix] = min(a)
I can also compare two vectors and get minimum values:
> min(a, b)
ans =
0
2
-5
But it is impossible to get positions of min values of two vectors:
> [x, ix] = min(a, b)
x =
0
2
-5
error: element number 2 undefined in return list
Why? How to get them? Is there a simple method?
回答1:
here's how to do that:
[v id]=min([a,b]')
回答2:
It's a matter of having the right insight:
[x,ix] = min([a b],[],2)
回答3:
You must think about what the intended output of ix is.
This shows you in which vector the minimum is:
ix=a<b;
x=a.*ix+b.*not(ix);
来源:https://stackoverflow.com/questions/20504954/octave-matlab-min-of-two-vectors