我已经有了这个代码的矢量和零和一些数字:
u=[1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0]
transitions=(find(u~=[u(2:end), u(end)+1]));
value=u(transitions)
transitions(2:end)=transitions(2:end)-transitions(1:end-1)
我明白了
value =
1 0 1 0 1 0
transitions =
5 3 2 7 5 3
现在,如果有人可以帮助我并解释如何在我的向量中获得百分比和零的百分比(所有这些以及每个值)。 非常感谢你。
答案 0 :(得分:3)
如果我理解正确,那很简单:
p1=sum(u)./numel(u); % is the percentage of ones
p2=1-p1; % is the percentage of zeros
Matlab甚至还有一个名为tabulate的特殊功能,它只为此创建一个频率表:
tabulate(u)
Value Count Percent
0 13 52.00%
1 12 48.00%
答案 1 :(得分:0)
这只是伪代码,所以它需要翻译,但这是一个潜在的解决方案:
total = 0;
total_ones = 0;
total_zeroes = 0;
for(i=0; i<transitions.length; i++){
total += transitions[i];
if(value[i] == 0)
total_zeroes += transitions[i];
else
total_ones += transitions[i];
}
print("Percent Zeroes: " + (total_zeroes/total)*100);
print("Percent Ones: " + (total_ones/total)*100);
#To print each group's percent it'd be along the lines of the following:
for(i=0; i<transitions.length; i++){
print("Percent of total comprised of this group: " + (transitions[i]/total)*100)
}