Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to check if a number is in an array?

Subject: How to check if a number is in an array?

From: HUI

Date: 28 Jan, 2008 06:29:02

Message: 1 of 8


the only way i can think is:

'sum(find(a==B))==0'

but i guess there should be a simple command,

isn't there?

Subject: How to check if a number is in an array?

From: Stefan

Date: 28 Jan, 2008 06:44:02

Message: 2 of 8

faster:

if any(abs(a-B)<1e-10),
      %...
end

or take a look for
help hist

Regards,
Stefan

ps. not tested




"HUI " <michaelyang0720@mathworks.com> wrote in message
<fnjsne$8on$1@fred.mathworks.com>...
>
> the only way i can think is:
>
> 'sum(find(a==B))==0'
>
> but i guess there should be a simple command,
>
> isn't there?

Subject: How to check if a number is in an array?

From: Jos

Date: 28 Jan, 2008 06:49:02

Message: 3 of 8

"HUI " <michaelyang0720@mathworks.com> wrote in message
<fnjsne$8on$1@fred.mathworks.com>...
>
> the only way i can think is:
>
> 'sum(find(a==B))==0'
>
> but i guess there should be a simple command,
>
> isn't there?



any(a==B)

or

any(abs(a-B)<tolerance)

hth
Jos

Subject: How to check if a number is in an array?

From: HUI

Date: 28 Jan, 2008 07:04:07

Message: 4 of 8

"Jos " <DELjos@jasenDEL.nl> wrote in message
<fnjtsu$kt2$1@fred.mathworks.com>...
> "HUI " <michaelyang0720@mathworks.com> wrote in message
> <fnjsne$8on$1@fred.mathworks.com>...
> >
> > the only way i can think is:
> >
> > 'sum(find(a==B))==0'
> >
> > but i guess there should be a simple command,
> >
> > isn't there?
>
>
>
> any(a==B)
>
> or
>
> any(abs(a-B)<tolerance)
>
> hth
> Jos

thx!

excellent!

Subject: How to check if a number is in an array?

From: Anand

Date: 13 Mar, 2010 22:23:05

Message: 5 of 8

really helpful

Thanks

Subject: How to check if a number is in an array?

From: Jan Simon

Date: 14 Mar, 2010 09:54:05

Message: 6 of 8

Dear Michael!

> the only way i can think is:
> 'sum(find(a==B))==0'

If this check is time-consuming in your program, you can try the C-Mex anyEq:
  http://www.mathworks.com/matlabcentral/fileexchange/26867

It is faster than ANY(a==B) if [B] has more than 1000 elements. Especially if [a] occures early in [B] the C-Mex can return immediately and do not have to serach the rest of [B].
If [B] is small, anyEq is slower due to the overhead of calling the Mex function.

Kind regards, Jan

Subject: How to check if a number is in an array?

From: Fran├žois

Date: 28 Jun, 2011 14:40:20

Message: 7 of 8

I know time went by since the last mail, but if people find this mail by a search engine :

what about the function ismember ? i.e.

 ismember([2;5],[1;3;5])

ans =

     0
     1




"Jan Simon" wrote in message <hnibnt$4hh$1@fred.mathworks.com>...
> Dear Michael!
>
> > the only way i can think is:
> > 'sum(find(a==B))==0'
>
> If this check is time-consuming in your program, you can try the C-Mex anyEq:
> http://www.mathworks.com/matlabcentral/fileexchange/26867
>
> It is faster than ANY(a==B) if [B] has more than 1000 elements. Especially if [a] occures early in [B] the C-Mex can return immediately and do not have to serach the rest of [B].
> If [B] is small, anyEq is slower due to the overhead of calling the Mex function.
>
> Kind regards, Jan

Subject: How to check if a number is in an array?

From: Jan Simon

Date: 1 Jul, 2011 10:00:13

Message: 8 of 8

Dear Fran├žois,

> what about the function ismember ? i.e.
> ismember([2;5],[1;3;5])

ISMEMBER sorts the input before searching the values of one element in the other. The overhead for sorting is remarkable, but the following binary search is fast. So for large arrays (lots of MB) this is fairly efficient.
A drawback is that the arrays are searched completely even if a single occurrence would be enough to stop the search. Then this wastes a lot of time:
  a = [2, rand(1, 1e5)];
  b = [2, rand(1, 1e5)];
  any(ismember(a, b))
1. The sorting needs an additional temporary vector with 1e7+1 elements.
2. The sorting takes time also.
3. The temporary LOGICAL vector replied by ISMEMBER needs memory also.
4. The complete input array is searched.

In this case the C-Mex function anyEq would be about 100 times faster. But if the two arrays do not share any common element, anyEq is 100 times slower. So I'm using anyEq, if one of the arrays is tiny (up to 8 elements).

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us