splitapply with multiple output variables

조회 수: 5 (최근 30일)
Charlotte Mock
Charlotte Mock 2023년 2월 23일
댓글: Matt J 2023년 2월 23일
Hi,
I would like to use splitapply in combination with the function mink on a column of a cell array. My code below gives me the correct number of minimum values for each of my groups, however, it does not give the indices for these values.
This is my code:
quantGroups = findgroups([locAwayFromMean{:,12}]); % grouping info as vector
funcMink = @(x) {mink(x,10)}; % anonymous fct to get 10 lowest values of x
valMink = splitapply(funcMink,cell2mat(locAwayFromMean(:,11)),quantGroups'); % apply fct to each group
I have a 258x12 cell (locAwayFromMean) where column 12 gives information about grouping. I want to get 10 minimum values (stored in column 11) for each group and also the indices of these data points.
The mink function [B,I] = mink(___) does allow to extract the indices but I am not sure how to implement this into my code above.
If I use this line
[valMink, Idx] = splitapply(funcMink,cell2mat(locAwayFromMean(:,11)),quantGroups');
it produces the following error message:
Error using splitapply
Applying the function '@(x){mink(x,10)}' to the 1st group of data generated the following error:
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Thanks in advance!

채택된 답변

Matt J
Matt J 2023년 2월 23일
편집: Matt J 2023년 2월 23일
quantGroups = findgroups([locAwayFromMean{:,12}]); % grouping info as vector
X=cell2mat(locAwayFromMean(:,11));
I=1:numel(X);
[valMink,indexMink] = splitapply(@funcMink, X(:), I(:), quantGroups(:)); % apply fct to each group
function [minval,minIndex]=funcMink(X,I)
[minval{1},j]=mink(X,10);
minIndex={I(j)};
end
  댓글 수: 6
Charlotte Mock
Charlotte Mock 2023년 2월 23일
Thank you, Matt!
P.S.: It appears that if I run your demo as an entire script everything works fine. However, if I run it line by line it produces the same error message "undefined function...". Not sure what that is about...
Matt J
Matt J 2023년 2월 23일
Well, you can't run it line by line, because one of those lines calls a function (funcMink) that Matlab needs to be able to find. You have to make the function visible to Matlab first.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by