确定向量中每个唯一元素的出现次数 [英] Determining the number of occurrences of each unique element in a vector
本文介绍了确定向量中每个唯一元素的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何确定MATLAB向量中某个值的相对频率?
How can I determine the relative frequency of a value in a MATLAB vector?
vector = [ 2 2 2 2 1 1 1 2 2 1 1 1 2 2 2 2 1 2 ];
哪个函数将返回每个唯一元素的出现次数?
What function will return the number of occurrences of each unique element?
推荐答案
您可以将unique
与histc
结合使用以获得相对频率.
You can use unique
in combination with histc
to get the relative frequency.
A=[1,2,3,1,2,4,2,1]; %#an example vector
unqA=unique(A);
这将唯一元素表示为unqA=[1,2,3,4]
.要获取发生次数,
This gives the unique elements as unqA=[1,2,3,4]
. To get the number of occurances,
countElA=histc(A,unqA); %# get the count of elements
relFreq=countElA/numel(A);
这给出了countElA=[3,3,1,1]
和relFreq=[0.3750, 0.3750, 0.1250, 0.1250]
,这是唯一元素的相对频率.这将适用于整数和浮点数.
This gives countElA=[3,3,1,1]
and relFreq=[0.3750, 0.3750, 0.1250, 0.1250]
, which is the relative frequency of the unique elements. This will work for both integers and floating points.
这篇关于确定向量中每个唯一元素的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文