Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave/Matlab: vectorising '==' operator?

I can look for the position of a value, i.e. 45, in a vector 'data' using the '==' operator and the 'find()' function:

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, i.e. i would like to get [ 4 5 7 ] in a single call:

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
like image 641
rmv Avatar asked Mar 03 '11 16:03

rmv


1 Answers

Try the ismember function:

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

find(ismember(data, values))

Giving:

ans =

 4     5     7     8
like image 127
Bill Cheatham Avatar answered Oct 05 '22 06:10

Bill Cheatham