简体   繁体   English

Octave / Matlab:vectorising'=='运算符?

[英]Octave/Matlab: vectorising '==' operator?

I can look for the position of a value, ie 45, in a vector 'data' using the '==' operator and the 'find()' function: 我可以使用'=='运算符和'find()'函数在向量'data'中查找值的位置,即45的位置:

data = [ 71 65 23 45 34 12 21 34 52 ];
value = 45;

find (data == value)
ans =  4

Is there a way to do the same for several values without using a loop, ie i would like to get [ 4 5 7 ] in a single call: 有没有办法在不使用循环的情况下为几个值做同样的事情,即我想在一次调用中得到[4 5 7]:

values = [ 45 34 21 ];
find (data == values)
error: mx_el_eq: nonconformant arguments (op1 is 1x9, op2 is 1x3)
error: evaluating argument list element number 1
error: evaluating argument list element number 1

Try the ismember function: 尝试使用ismember函数:

data = [ 71 65 23 45 34 12 21 34 52 ];
values = [ 45 34 21 ];

find(ismember(data, values))

Giving: 赠送:

ans =

 4     5     7     8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM