필터 지우기
필터 지우기

Calculating percent of data in table

조회 수: 96 (최근 30일)
BN
BN 2020년 4월 28일
댓글: Tommy 2020년 4월 29일
I have a 1x12 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 %
  댓글 수: 2
Adam Danz
Adam Danz 2020년 4월 28일
"I have a 1x12 table,"
So you have a table with 1 column? The rest of your question sounds like you have >1 column.
BN
BN 2020년 4월 29일
I'm sorry you right; I have a table like this:

댓글을 달려면 로그인하십시오.

채택된 답변

Tommy
Tommy 2020년 4월 28일
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
  댓글 수: 2
BN
BN 2020년 4월 29일
Thank you, It workes great. I forgot to attach a sample, Did you know-how about when I have such a data set?
I tried to change the code to works, but always I got an error
'operator '*' is not supported for operands of type 'table'
Thanks again
Tommy
Tommy 2020년 4월 29일
For that table, you could use { } indexing to obtain the cells within and then cell2mat to convert to a double array:
arr = cell2mat(data{1,:});
percentages = 100 * arr ./ sum(arr);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by