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 index and value of duplicates in a vector

Subject: find index and value of duplicates in a vector

From: George34 Dampf

Date: 11 Nov, 2011 16:40:32

Message: 1 of 3

Hello,
I need to find the indices and the values of duplicates in a vector.
e.g. a = [ 5 3 1 5 6]
So I want to find
value = 5
and
inds = [1 4]
I've tried a lot with unique, sort and diff, but till now I wasnt able to get what i need.
Any help appreciated! Thanks in advance!

Subject: find index and value of duplicates in a vector

From: dpb

Date: 11 Nov, 2011 17:15:47

Message: 2 of 3

On 11/11/2011 10:40 AM, George34 Dampf wrote:
> Hello,
> I need to find the indices and the values of duplicates in a vector.
> e.g. a = [ 5 3 1 5 6]
> So I want to find value = 5 and inds = [1 4]
> I've tried a lot with unique, sort and diff, but till now I wasnt able
> to get what i need.
> Any help appreciated! Thanks in advance!

Hints...

 >> a = [ 5 3 1 5 6];
 >> u=unique(a);
 >> n=histc(a,u)
n =
      1 1 2 1
 >> u(n>1)
ans =
      5
 >> find(a==u(n>1))
ans =
      1 4

If there are more than one sets of duplicates, look at all bins>1 entry


--

Subject: find index and value of duplicates in a vector

From: George34 Dampf

Date: 11 Nov, 2011 17:59:31

Message: 3 of 3

Thank you so much, this helped a lot!!

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