Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate most common values

If i have a matrix A with n values spanning from 65:90. How do i get the 10 most common values in A? I want the result to be a 10x2 matrix B with the 10 common values in the first column and the times it appears in the second column.

like image 850
gustav Avatar asked Jan 22 '23 22:01

gustav


1 Answers

A = [65 82 65 90; 90 70 72 82]; % Your data
range = 65:90;
res = [range; histc(A(:)', range)]'; % res has values in first column, counts in second.

Now all you’ve got to do is sort the res array by the second column and take the first 10 rows.

sortedres = sortrows(res, -2); % sort by second column, descending
first10 = sortedres(1:10, :)
like image 126
Debilski Avatar answered Jan 30 '23 08:01

Debilski