How to identify array elements that occur more than once ?

조회 수: 28 (최근 30일)
pavan sunder
pavan sunder 2016년 12월 21일
댓글: Juan Velasquez 2021년 1월 28일
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..

답변 (2개)

KSSV
KSSV 2016년 12월 21일
편집: KSSV 2016년 12월 21일
Read about unique
find(hist(D,unique(D))>1)
  댓글 수: 1
Stephen23
Stephen23 2016년 12월 21일
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.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2016년 12월 21일
>> U = unique(D);
>> U(1<histc(D,unique(D)))
ans =
3 7

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by