2011-03-22 117 views

回答

31

您可以結合使用uniquehistc獲得的相對頻率。

A=[1,2,3,1,2,4,2,1]; %#an example vector 
unqA=unique(A); 

這給出了獨特的元素unqA=[1,2,3,4]。要獲得出現的次數,

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],這是唯一的元素的相對頻率。這將適用於整數和浮點。

10

對於您有浮點值的向量的最一般的情況下,可以使用的功能UNIQUEACCUMARRAY

[uniqueValues,~,uniqueIndex] = unique(vector); 
frequency = accumarray(uniqueIndex(:),1)./numel(vector); 
+0

thanks for accumarray – 2014-11-21 03:44:07

4

您可以使用功能tabulate。用你的矢量來看這個例子。

vector = [ 2 2 2 2 1 1 1 2 2 1 1 1 2 2 2 2 1 2 ]; 
tabulate(vector); 
    Value Count Percent 
     1  7  38.89% 
     2  11  61.11% 

如果需要它在百分之順序,執行:

t = tabulate(vector); 
t = sortrows(t, 3) 
0

this答案參考:

unqV = unique(vector); 
y = zeros(size(unqV)); 
for i = 1:length(unqV) 
    y(i) = sum(unqV(i)==vector); 
end 

unqV = [1 2]
Y = [7 11]

相關問題