rmv*_*rmv 8 matlab operators vectorization find octave
我可以使用'=='运算符和'find()'函数在向量'data'中查找值的位置,即45的位置:
data = [ 71 65 23 45 34 12 21 34 52 ];
value = 45;
find (data == value)
ans = 4
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用循环的情况下为几个值做同样的事情,即我想在一次调用中得到[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
Run Code Online (Sandbox Code Playgroud)
Bil*_*ham 14
尝试使用ismember函数:
data = [ 71 65 23 45 34 12 21 34 52 ];
values = [ 45 34 21 ];
find(ismember(data, values))
Run Code Online (Sandbox Code Playgroud)
赠送:
ans =
4 5 7 8
Run Code Online (Sandbox Code Playgroud)