How to use splitappy for only 10 rows in each group?

3 ビュー (過去 30 日間)
maswbk
maswbk 2021 年 6 月 21 日
コメント済み: maswbk 2021 年 6 月 21 日
Hello,
I have a table wit numerous columns and rows (resultsT = 2795x23 table)
I want to use the split apply for calculating means and standard deviations on groups, separated with the help of findgroups over several variables from my table. OK, pretty simple .. BUT I want to do the calculations only on 10 rows for each group (random rows). Some groups have less then 10 rows (in this case dismiss, or whatever), and the most groups have 13 or 15 rows.
Anyone have an idea that could help?
Here is my code for the calculations for all the values whitin the groups:
[G,MessungError,Markiert,KalError,Calibrator,Tag]=findgroups(resultsT.MessungError,resultsT.Markiert,resultsT.KalError,resultsT.Calibrator,resultsT.Tag);
meanS1=splitapply(@(x) mean(x,1),resultsT.kSignal_1,G);
stdS1=splitapply(@(x) std(x,0,1),resultsT.kSignal_1,G);
Thank you very much in advance,
Marina

採用された回答

dpb
dpb 2021 年 6 月 21 日
You can't (at least without more machinations than I'd care to even attempt) do that with an anonymous function, but since the result is still a single row, it's simple-enough to write a function to do it --
function mn=mymean(x)
% return mean by column of x for random selection of maximum of 10 rows;
% fewer rows than 10 will return NaN for missing value indicator
[r,c]=size(x); % get size of group input
if r<10
mn=nan(1,c);
else
mn=mean(x(randperm(r,10),:));
end
end
Use as
meanS1=rowfun(@mymean,resultsT,'inputVariables','kSignal','groupingVariables',{'MessungError','Markiert','KalError','Calibrator','Tag'});
Extend to std deviation similarly, or; see the example in the doc for rowfun to return multiple outputs from a function (it shows mean and std as well).
Or, see grpsummary -- it can also use a custom function
  1 件のコメント
maswbk
maswbk 2021 年 6 月 21 日
Oh thank you very much, it works perfectly!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by