Thread Subject: find the same number in two array

Subject: find the same number in two array

From: Arthur Zheng

Date: 30 Jul, 2009 17:37:03

Message: 1 of 6

there are two arrays, e.g.
arr1 = [ 1 2 3 4 5 6];
arr2 = [ 2 4 3];

What is an efficient way to calculate the number of common numbers in the two arrays? Is there such a function? In the above example, the result should be 3, since the common numbers are: 2, 3, 4.

Subject: find the same number in two array

From: Nathan

Date: 30 Jul, 2009 17:42:31

Message: 2 of 6

On Jul 30, 10:37 am, "Arthur Zheng" <hzhe...@gatech.edu> wrote:
> there are two arrays, e.g.
> arr1 = [ 1 2 3 4 5 6];
> arr2 = [ 2 4 3];
>
> What is an efficient way to calculate the number of common numbers in the two arrays? Is there such a function? In the above example, the result should be 3, since the common numbers are: 2, 3, 4.

length(intersect(arr1,arr2))


-Nathan

Subject: find the same number in two array

From: Arthur Zheng

Date: 30 Jul, 2009 18:09:01

Message: 3 of 6

Nathan <ngreco32@gmail.com> wrote in message <89daed8f-85a6-4046-9f72-58f5ffe5eab7@u16g2000pru.googlegroups.com>...
> On Jul 30, 10:37?am, "Arthur Zheng" <hzhe...@gatech.edu> wrote:
> > there are two arrays, e.g.
> > arr1 = [ 1 2 3 4 5 6];
> > arr2 = [ 2 4 3];
> >
> > What is an efficient way to calculate the number of common numbers in the two arrays? Is there such a function? In the above example, the result should be 3, since the common numbers are: 2, 3, 4.
>
> length(intersect(arr1,arr2))
>
>
> -Nathan

thank you Nathan

Subject: find the same number in two array

From: Jan Simon

Date: 31 Jul, 2009 01:22:03

Message: 4 of 6

"Arthur Zheng" <hzheng7@gatech.edu> wrote in message <h4slnv$9dj$1@fred.mathworks.com>...
> there are two arrays, e.g.
> arr1 = [ 1 2 3 4 5 6];
> arr2 = [ 2 4 3];
>
> What is an efficient way to calculate the number of common numbers in the two arrays? Is there such a function? In the above example, the result should be 3, since the common numbers are: 2, 3, 4.

Nathan's "length(intersect(arr1, arr2))" fails, if elements are repeated:
  intersect([1,2,3,4,5,6,2], [2,4,3])
  => [2, 3, 4]

For repetitions:
  sum(ismember(arr1, arr2))

If efficient means high performance for small vectors (e.g. if called millions of times), you can avoid the offset of ISMEMBER:
  sum(ismembc(sort(arr1), sort(arr2)))
For large arrays (> 100.000 elements) calling the MEX directly is not a big advantage. But if you know, the arrays are sorted already, you could save the time for sorting them again.

Good luck, Jan

Subject: find the same number in two array

From: Matt Fig

Date: 31 Jul, 2009 03:03:01

Message: 5 of 6

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
> Nathan's "length(intersect(arr1, arr2))" fails, if elements are repeated:



That is debatable. The OP would have to define what is meant by:
"number of common numbers in the two arrays"
I could see it either way.



> For repetitions:
> sum(ismember(arr1, arr2))
>
> If efficient means high performance for small vectors (e.g. if called millions of times), you can avoid the offset of ISMEMBER:
> sum(ismembc(sort(arr1), sort(arr2)))
> For large arrays (> 100.000 elements) calling the MEX directly is not a big advantage. But if you know, the arrays are sorted already, you could save the time for sorting them again.
>
> Good luck, Jan




ISMEMBC only needs the second argument to be sorted, and since the OP said nothing about vectors, that should be:

sum(ismembc(arr1(:), sort(arr2(:))))

Subject: find the same number in two array

From: Jan Simon

Date: 6 Aug, 2009 00:47:02

Message: 6 of 6

Dear Matt Fig!

> > "length(intersect(arr1, arr2))" fails, if elements are repeated:
> That is debatable.
Of course, this depends on the problem.

> ISMEMBC only needs the second argument to be sorted, and since the OP said nothing about vectors, that should be:
> sum(ismembc(arr1(:), sort(arr2(:))))
Absolutely: sorting the first array is senseless, sorry. In addition ISMEMBC does not like NaNs.

Thanks, Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
number array co... Arthur Zheng 30 Jul, 2009 13:39:04
rssFeed for this Thread

Contact us at files@mathworks.com