Octave / Matlab: vector operator '=='?

I can find the position of the value, i.e. 45, in 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 multiple values ​​without using a loop, that is, I would like to get [4 5 7] in one 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 
+8
operators vectorization find matlab octave
source share
1 answer

Try the ismember function:

 data = [ 71 65 23 45 34 12 21 34 52 ]; values = [ 45 34 21 ]; find(ismember(data, values)) 

Donation:

 ans = 4 5 7 8 
+14
source share

All Articles