check whether a number belongs to an array or not

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?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Dez. 2011

21 Stimmen

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 Kommentare

i guess "ismember" is for vectors,nor for array.but i am intrested to check whether a number belongs to an array or not.can you please give me an idea?
ismember(theValue, YourArray(:))
thanks,let me try.
thanks,it works
Note: later versions of MATLAB added ismembertol() for dealing with floating point numbers.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 28 Dez. 2011

16 Stimmen

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 Kommentare

Hello K. Jan Simon
assume i have many group of array that i want to check it contain 5 whether or not ha within loop. For example: first time, i check A array that it contain 5 yes or not and next, i check B and C, respectively. How i can modify your code? please show sample.
A = [1 10 12 5 4 15 29];
B = [2 23 5 19 28 1 3];
C = [23 5 1 6 29 9];
Thank you every much
Wasikar
Wasikar am 7 Mär. 2017
Bearbeitet: Walter Roberson am 8 Mär. 2017
l_array = [A,B,C];
res = ismember(5,l_array);
disp(res)
ha ha
ha ha am 30 Mär. 2018
@Jan. Can you teach me how to run FEX: anyEq. as you mentioned? Thanks
- 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by