Matlab学习手记——计算整数序列的元素概率分布

版权声明:转载请标明出处 https://blog.csdn.net/u012366767/article/details/81703250

函数功能:获取一组整数序列的元素出现的频率。

源码:

function [x_uniq, dist] = Number_Distribution(x)
x_uniq = unique(x);  
dist = zeros(length(x_uniq),1);  
for i = 1:length(x_uniq)
  dist(i) = sum(x==x_uniq(i)) / length(x);
end

    主要用到unique函数来确定序列中不重复的元素,然后遍历一下这些不重复的元素。

x=[1 2 3 4 5 1 1 2];
[x_uniq, dist] = Number_Distribution(x)

x_uniq =
     1     2     3     4     5
dist =
    0.3750
    0.2500
    0.1250
    0.1250
    0.1250

猜你喜欢

转载自blog.csdn.net/u012366767/article/details/81703250