views:

320

answers:

3

Can anyone tell me if there is a way (in MATLAB) to check whether a certain value is equal to any of the values stored within another array?

The way I intend to use it is to check whether an element index in one matrix is equal to the values stored in another array (where the stored values are the indices of the elements which meet a certain criteria).

So, if the indices of the elements which meet the criteria are stored in the matrix below:

criteriacheck = [3 5 6 8 20];

Going through the main array (called array) and checking if the index matches:

for i = 1:numel(array)
  if i == 'Any value stored in criteriacheck'
    %# "Do this"
  end
end

Does anyone have an idea of how I might go about this?

A: 

you could use the find command

if (~isempty(find(criteriacheck == i)))
    % do something
end
groovingandi
+5  A: 

Many ways to do this. ismember is the first that comes to mind, since it is a set membership action you wish to take. Thus

X = primes(20);
ismember([15 17],X)
ans =
      0    1

Since 15 is not prime, but 17 is, ismember has done its job well here.

Of course, find (or any) will also work. But these are not vectorized in the sense that ismember was. We can test to see if 15 is in the set represented by X, but to test both of those numbers will take a loop, or successive tests.

~isempty(find(X == 15))
~isempty(find(X == 17))

or,

any(X == 15)
any(X == 17)

Finally, I would point out that tests for exact values are dangerous if the numbers may be true floats. Tests against integer values as I have shown are easy. But tests against floating point numbers should usually employ a tolerance.

tol = 10*eps;
any(abs(X - 3.1415926535897932384) <= tol)
Pentium10
+1  A: 

Note: Although this answer doesn't address the question in the title, it does address a more fundamental issue with how you are designing your for loop (the solution of which negates having to do what you are asking in the title). ;)

Based on the for loop you've written, your array criteriacheck appears to be a set of indices into array, and for each of these indexed elements you want to do some computation. If this is so, here's an alternative way for you to design your for loop:

for i = criteriacheck
  %# Do something with array(i)
end

This will loop over all the values in criteriacheck, setting i to each subsequent value (i.e. 3, 5, 6, 8, and 20 in your example). This is more compact and efficient than looping over each element of array and checking if the index is in criteriacheck.

NOTE: As Jonas points out, you want to make sure criteriacheck is a row vector for the for loop to function properly. You can form any matrix into a row vector by following it with the (:)' syntax, which reshapes it into a column vector and then transposes it into a row vector:

for i = criteriacheck(:)'
...
gnovice
Note that criteriacheck needs to be a row vector, i.e. [3,5,6...], instead of [3;5;6...]. To be safe, I'd write <<for i=criteriacheck(:)'>>. At any rate, +1
Jonas
@Jonas: Good idea, just to be safe. ;)
gnovice

related questions