7

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?

0

5 Answers 5

19

The excellent answer previously given by @woodchips applies here as well:

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)
Sign up to request clarification or add additional context in comments.

Comments

3

you could use the find command

if (~isempty(find(criteriacheck == i)))
    % do something
end

Comments

3

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(:)'
...

2 Comments

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: Good idea, just to be safe. ;)
1

The original question "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?" can be solved without any loop.

Just use the setdiff function.

Comments

0

I think the INTERSECT function is what you are looking for.

C = intersect(A,B) returns the values common to both A and B. The values of C are in sorted order.

http://www.mathworks.de/de/help/matlab/ref/intersect.html

The question if i == 'Any value stored in criteriacheck can also be answered this way if you consider i a trivial matrix. However, you are proably better off with any(i==criteriacheck)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.