MATLAB Answers

check whether a number belongs to an array or not

5,584 views (last 30 days)
hi all, i want to check a number in the array.i have created an array and i want to know that a whether a perticular number belongs to the array or not. is there any inbuild function for "belongs to" in matlab. can anybody help me?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Dec 2011
ismember()
Note: if you are not using purely whole numbers, then please read http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

  5 Comments

Show 2 older comments
Walter Roberson
Walter Roberson on 30 Mar 2018
Note: later versions of MATLAB added ismembertol() for dealing with floating point numbers.

Sign in to comment.

More Answers (1)

Jan
Jan on 28 Dec 2011
A = floor(rand(100, 100) * 1000);
contains5 = any(A(:) == 5);
If the array is large and the searched element is found early, Matlab wastes a lot of time by searching the rest of the array. Then this C-Mex is faster: FEX: anyEq.

  4 Comments

Show 1 older comment
Wasikar
Wasikar on 7 Mar 2017
l_array = [A,B,C];
res = ismember(5,l_array);
disp(res)
ha ha
ha ha on 30 Mar 2018
@Jan. Can you teach me how to run FEX: anyEq. as you mentioned? Thanks
James Tursa
James Tursa on 30 Mar 2018
- Download the code from the provided link
- Compile the code at the command line:
mex anyEq.c -largeArrayDims
- Use the function like any other function. For help on syntax,
help anyEq

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!