计算向量中零和一的百分比? [英] Calculate percentage of zeros and ones in my vector?

查看:126
本文介绍了计算向量中零和一的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了此代码的矢量和零以及一的数量:

I've already have my vector and number of zeros and ones with this code:

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

现在,请问是否有人可以帮助我,并解释一下我如何在向量中获得1的百分比和0的百分比(所有值,以及每个值). 非常感谢你.

Now, please if someone could help me and explain how I can get percentage of ones and percentage of zeros in my vector (all together, and by each value). Thank You very much.

推荐答案

如果我对您的理解正确,那很简单:

If I understand you correctly it is very simple:

p1=sum(u)./numel(u); % is the percentage of ones
p2=1-p1;             % is the percentage of zeros   

Matlab甚至有一个称为制表的特殊功能,可以为此创建频率表:

Matlab has even this spesific function called tabulate that creates a frequency table just for that:

tabulate(u)

Value    Count   Percent
      0       13     52.00%
      1       12     48.00%

这篇关于计算向量中零和一的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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