Calculate percentage of zeros and ones in my vector?

前端 未结 2 1275
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 02:32

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),          


        
2条回答
  •  生来不讨喜
    2021-01-25 03:27

    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 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%
    

提交回复
热议问题