Calculate percentage of zeros and ones in my vector?

前端 未结 2 1276
被撕碎了的回忆
被撕碎了的回忆 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%
    
    0 讨论(0)
  • 2021-01-25 03:28

    This is just psuedocode so it'll need to be translated, but here's a potential solution:

    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)
    }
    
    0 讨论(0)
提交回复
热议问题