필터 지우기
필터 지우기

How to calculate count of matching rows and average of specified column of all matching rows

조회 수: 7 (최근 30일)
Hi,
I have cell array matrix as below:
Day Name Score Status
2018-01-02 23:04:45 VAH 23 Success
2018-04-05 13:44:15 BHT 23 Success
2018-05-02 14:34:56 HDA07H 12 Success
2018-05-12 09:34:56 PHA07H 3 Failure
2018-05-12 09:34:56 HJA07H 34 Failure
2018-05-23 09:23:46 HOA07H 47 Success
I want to count the total count of "Success" cases and the average score of success cases in column3
Day Name Score Status
2018-01-02 23:04:45 VAH 23 Success
2018-04-05 13:44:15 BHT 23 Success
2018-05-02 14:34:56 HDA07H 12 Success
2018-05-23 09:23:46 HOA07H 47 Success
My desired output is:
SuccesCount AvgSuccessScore
4 26.25
I use below code: do in 4 steps, but can I get it one step?
Index= strfind(input(:,4), 'Success');
Index = find(not(cellfun('isempty', Index)));
AvgSuccessScore=mean(input(Index,3))
SuccesCount=size(Index,1);
  댓글 수: 1
Paolo
Paolo 2018년 6월 9일
Are two steps acceptable?
SuccessCount = nnz(strcmp(cellA(:,4), 'Success'));
AvgSuccessScore=mean(cell2mat(cellA(strcmp(cellA(:,4),'Success'),3)));

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

답변 (1개)

Image Analyst
Image Analyst 2018년 6월 9일
I believe so. First, don't use a cell array, use a table. Cell array is not the best form for this data - table is much better. Then simply call grpstats() if you have the Statistics and Machine Learning Toolbox.
  댓글 수: 2
Mekala balaji
Mekala balaji 2018년 6월 9일
Sir,
My Matlab verision is R2012b, is there any way to simplify my code
Image Analyst
Image Analyst 2018년 6월 9일
See how grpstats() works with your antique (pre-table) version. The function was there back then and worked somehow. Maybe just use cell2mat() to extract the numerical parts into a matrix.

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

Community Treasure Hunt

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

Start Hunting!

Translated by