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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by