I can use unique(A) to get an array of each unique value in the matrix A. However, how can I also get an array of the number of elements with each unique value? For instance
B=unique(A) might give:
[1; 4; 6; 8];
and an additional step might give:
[10; 1; 5; 7];
if there were 10 entries of 1, 1 entry of 4, 5 entries of 6, etc.
I know I can easily do this by doing find(A==B(1,1))... etc. but if the array of uniques is long then this should be very inefficient.
Is there a function like unique which returns the number of elements for each unique value without much additional computation??
Thanks

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 10월 15일
B = unique(A);
out = [B,histc(A,B)];

댓글 수: 3

Christopher
Christopher 2014년 10월 15일
편집: Christopher 2014년 10월 15일
I don't understand the output of 'out'? It's definitely not a simple list of corresponding amounts of elements with certain unique values.
Edit:
I think this is what I am looking for:
out=sum(histc(O,B),2)
It should be, Christopher.
A = [1;1;1;2;3;3];
B = unique(A);
out = [B,histc(A,B)]
out =
1 3
2 1
3 2
First column is value, second is number of occurrences

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2014년 10월 15일

댓글:

2014년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by