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

5 ビュー (過去 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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by