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:
find common elements in two arrays matlab

Subject: find common elements in two arrays matlab

From: Saad

Date: 30 Oct, 2012 16:43:09

Message: 1 of 4

Dear all,

I have two arrays of different size say A(nx1) and B(mx1) where n differs from m. I would like to find the elements of B that are as close as possible to the element of A and display them. How can I do that please?
 Most of matlab applications I found (equal, ismember, find, etc) they require arrays of the same size which is not my case. Thank you very much for any guidance or advice you could give me

Regards

S

Subject: find common elements in two arrays matlab

From: dpb

Date: 30 Oct, 2012 17:38:30

Message: 2 of 4

On 10/30/2012 11:43 AM, Saad wrote:
> Dear all,
>
> I have two arrays of different size say A(nx1) and B(mx1) where n
> differs from m. I would like to find the elements of B that are as close
> as possible to the element of A and display them. How can I do that please?
> Most of matlab applications I found (equal, ismember, find, etc) they
> require arrays of the same size which is not my case. Thank you very
> much for any guidance or advice you could give me

Pretty ill-posed problem; you'll need to refine the criterion to
actually implement anything specific but I'd think a combination of
unique() on each array and a pairwise difference operation on those
would come into play...

I don't think there's any oneliner that will solve this one.

--

Subject: find common elements in two arrays matlab

From: Matt J

Date: 30 Oct, 2012 18:19:08

Message: 3 of 4

"Saad" wrote in message <k6p02t$dg1$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I have two arrays of different size say A(nx1) and B(mx1) where n differs from m. I would like to find the elements of B that are as close as possible to the element of A and display them. How can I do that please?
> Most of matlab applications I found (equal, ismember, find, etc) they require arrays of the same size which is not my case. Thank you very much for any guidance or advice you could give me

ismember does not require arrays of the same size. The following is the same, but will let you apply a tolerance.

http://www.mathworks.com/matlabcentral/fileexchange/23294-ismemberf

Subject: find common elements in two arrays matlab

From: Jos (10584)

Date: 31 Oct, 2012 10:22:08

Message: 4 of 4

"Saad" wrote in message <k6p02t$dg1$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I have two arrays of different size say A(nx1) and B(mx1) where n differs from m. I would like to find the elements of B that are as close as possible to the element of A and display them. How can I do that please?
> Most of matlab applications I found (equal, ismember, find, etc) they require arrays of the same size which is not my case. Thank you very much for any guidance or advice you could give me
>
> Regards
>
> S

So, you want to obtain a matrix C that has the same size as A (nx1) for which an element C(k) contains that value of B that is the nearest to A(k), for k is 1 to n ...

My NEARESTPOINT does exactly that:
http://www.mathworks.com/matlabcentral/fileexchange/8939

idx = nearestpoint(A,B) ;
C = B(idx)

or you can play around with interp1 ...

~ Jos

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