number of elements of each unique values in a matrix

16 Ansichten (letzte 30 Tage)
Christopher
Christopher am 15 Okt. 2014
Kommentiert: Sean de Wolski am 15 Okt. 2014
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

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 15 Okt. 2014
B = unique(A);
out = [B,histc(A,B)];
  3 Kommentare
Sean de Wolski
Sean de Wolski am 15 Okt. 2014
It should be, Christopher.
Sean de Wolski
Sean de Wolski am 15 Okt. 2014
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Install Products finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by