Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding whether a value is equal to the value of any array element in MATLAB

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?

like image 977
James Avatar asked Mar 14 '10 15:03

James


People also ask

How do you check if a value is equal to a value in an array?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.

How do you check if two values in an array are equal in MATLAB?

tf = isequal( A,B ) returns logical 1 ( true ) if A and B are equivalent; otherwise, it returns logical 0 ( false ).

How do you check if something is equal in MATLAB?

A == B returns a logical array with elements set to logical 1 ( true ) where arrays A and B are equal; otherwise, the element is logical 0 ( false ). The test compares both real and imaginary parts of numeric arrays.

How do you know if two values in an array are equal?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.


1 Answers

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)
like image 117
Pentium10 Avatar answered Sep 25 '22 00:09

Pentium10