MATLAB: Calculating percent of data in table

MATLAB

I have a 1×12 table, Is there any quick way or function to find percentage for each column based on total?
for example:
data = (1,2,3)
percent first = (1*100)/(1+2+3) = 16.66 %
percent two = (2*100)/(1+2+3) = 33.33 %

Best Answer

  • For the following table:
    T = table;
    T.data = randi(10,1,12);
    you can use:
    T.percentages = 100 * T.data ./ sum(T.data);
    which gives:
    >> T.data
    ans =
    10 1 9 2 5 3 2 3 9 10 4 9
    >> T.percentages
    ans =
    14.9254 1.4925 13.4328 2.9851 7.4627 4.4776 2.9851 4.4776 13.4328 14.9254 5.9701 13.4328
    and to be sure:
    >> sum(T.percentages)
    ans =
    100