How to get statistical summary

조회 수: 1 (최근 30일)
Mekala balaji
Mekala balaji 2018년 10월 16일
편집: Guillaume 2018년 10월 16일
Hi,
I have below cell array,
Step 34
Step 56
Double 23
Stop 123
I want to get the summation and count of each category:
Desired output:
Step 90 2
Double 23 1
Stop 123 1
Continue 0 0
Here in the above example, no continue, but in some of my cases it exists. So for each scenario, I want to get the summation & count of: Step Double Stop Continue

채택된 답변

Guillaume
Guillaume 2018년 10월 16일
The way I'd do this is first convert your cell array to a table whose first column is of type categorical:
democell = {'Step', 34; 'Double', 56; 'Step', 23; 'Stop', 123}
demotable = table(categorical(democell(:, 1), {'Step', 'Double', 'Stop', 'Continue'}), cell2mat(democell(:, 2)), 'VariableNames', {'category', 'value'})
Your summation is then trivial:
varfun(@sum, demotable, 'GroupingVariables', 'category', 'InputVariables', 'value')
  댓글 수: 2
Mekala balaji
Mekala balaji 2018년 10월 16일
Output is still:
Step 2 57
Double 1 56
Stop 1 123
But I still need Continue sum is 0, count is 0.
Guillaume
Guillaume 2018년 10월 16일
편집: Guillaume 2018년 10월 16일
Then, you'd have to use more or less Kevin's answer. I'd still stuff the input into a table and change the first column to categorical as it's easier to work with. I'd do the calculation like this:
catnames = categories(demotable.category)
[~, id] = ismember(demotable.category, catnames);
result = table(catnames, accumarray(id, 1, [numel(catnames), 1]), accumarray(id, demotable.value, [numel(catnames), 1]), 'VariableNames', {'category', 'count', 'sum'})

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

추가 답변 (1개)

Kevin Chng
Kevin Chng 2018년 10월 16일
편집: Kevin Chng 2018년 10월 16일
A = ["Step" "Step" "Double" "Stop" "Continue"];
B = [ 34 56 23 123 0]
[c,~,d] = unique(A);
out = [c',accumarray(d,B),countcats(categorical(A))']
  댓글 수: 2
Mekala balaji
Mekala balaji 2018년 10월 16일
I am getting below output, but it giving continue sum is 1, count 0. In fact the Continue should have 0 &0
Continue 0 1
Double 23 1
Step 90 2
Stop 123 1
Kevin Chng
Kevin Chng 2018년 10월 16일
편집: Kevin Chng 2018년 10월 16일
change to
out = table(c',accumarray(d,B),[zeros(numel(A(B==0)))';countcats(categorical(A(B>0)))'])

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by