计算最常见的值 [英] Calculate most common values

查看:129
本文介绍了计算最常见的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个矩阵A,其n值跨越65:90。如何获得A中最常见的10个值?我想要的结果是一个10x2矩阵B与第一列中的10个通用值和出现在第二列的时间。

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.

推荐答案

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.

现在所有你需要做的是将 res 第二列的数组,并取前10行。

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, :)

这篇关于计算最常见的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆