How to find most frequent value from a Matrix column?
4 次查看(过去 30 天)
显示 更早的评论
We can find one most frequent value from a row or col using the mode function. Anyone please guide me how we can find two or more frequent values from a single row or col. for example A=[ 1; 1; 1; 1; 1; 1; 2; 2; 2; 2; 2; 3; 3; 5; 5; 5; 5; 6; 7; 8;]
In the above example, 1st most frequent value is 1, 2nd most frequent value is 2, and 3rd most frequent value is 5,
So, how we can find the values as 1st most frequent, 2nd, 3rd and so on
0 个评论
采纳的回答
dpb
2017-2-23
编辑:dpb
2017-2-23
>> u=unique(A); % find the unique values in vectr
>> [n,b]=histc(A,u); % bin on those and count number in bin
>> [n,is]=sort(n,'descend'); % sort highest first and keep index array of position
>> m=A(arrayfun(@(x) find(b==x,1,'first'),is)); % find the values in original array corresponding
>> [n m] % display the results of number, corresponding value
ans =
6 1
5 2
4 5
2 3
1 6
1 7
1 8
>>
ADDENDUM: To only return N-highest, simply limit the range of the subscripts supplied in arrayfun to only process the number desired instead of entire set.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!