How to identify array elements that occur more than once ?

28 Ansichten (letzte 30 Tage)
pavan sunder
pavan sunder am 21 Dez. 2016
Kommentiert: Juan Velasquez am 28 Jan. 2021
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..

Antworten (2)

KSSV
KSSV am 21 Dez. 2016
Bearbeitet: KSSV am 21 Dez. 2016
Read about unique
find(hist(D,unique(D))>1)
  1 Kommentar
Stephen23
Stephen23 am 21 Dez. 2016
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.

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 21 Dez. 2016
>> U = unique(D);
>> U(1<histc(D,unique(D)))
ans =
3 7

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by