Octave/Matlab: min of two vectors

随声附和 提交于 2019-12-25 00:33:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!