MATLAB Answers

0

How to identify array elements that occur more than once ?

Asked by pavan sunder on 21 Dec 2016
Latest activity Commented on by Stephen Cobeldick on 21 Dec 2016
For example D=[1 2 3 3 3 4 5 6 7 7 8]
I want to identify the elements that occur more than once.
The expected answer with respect to the above example Would be [ 3 7].
How do i do it in matlab. I can use for loop but i want to find out is there a better approach..

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by KSSV
on 21 Dec 2016
Edited by KSSV
on 21 Dec 2016

Read about unique
find(hist(D,unique(D))>1)

  1 Comment

This answer does not return the values as requested, e.g.:
>> D = [2 3 3 3 4 5 6 7 7 8];
>> find(hist(D,unique(D))>1)
ans =
2 6
See my answer for the correct code.

Sign in to comment.


Answer by Stephen Cobeldick on 21 Dec 2016

>> U = unique(D);
>> U(1<histc(D,unique(D)))
ans =
3 7

  0 Comments

Sign in to comment.