splitapply with multiple output variables

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!

 Accepted Answer

Matt J
Matt J on 23 Feb 2023
Edited: Matt J on 23 Feb 2023
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 Comments

Thank you for your reply. Unfortunately, your code produces this error for me:
Error using splitapply
Applying the function 'funcMink' to the 1st group of data generated the following error:
Undefined function 'funcMink' for input arguments of type 'double'.
But you can see that the function is defined...
It does not appear in the workspace and looking for it with which gives me the message "'funcMink' not found.". Looking for it with exist similarly results in "ans = 0". Any ideas what I can do?
Here is a demo:
locAwayFromMean=reshape(1:30*12,30,12);
locAwayFromMean(:,12)=repelem([1;2],15)
locAwayFromMean = 30×12
1 31 61 91 121 151 181 211 241 271 301 1 2 32 62 92 122 152 182 212 242 272 302 1 3 33 63 93 123 153 183 213 243 273 303 1 4 34 64 94 124 154 184 214 244 274 304 1 5 35 65 95 125 155 185 215 245 275 305 1 6 36 66 96 126 156 186 216 246 276 306 1 7 37 67 97 127 157 187 217 247 277 307 1 8 38 68 98 128 158 188 218 248 278 308 1 9 39 69 99 129 159 189 219 249 279 309 1 10 40 70 100 130 160 190 220 250 280 310 1
locAwayFromMean=num2cell(locAwayFromMean);
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
valMink{:}
ans = 10×1
301 302 303 304 305 306 307 308 309 310
ans = 10×1
316 317 318 319 320 321 322 323 324 325
indexMink{:}
ans = 10×1
1 2 3 4 5 6 7 8 9 10
ans = 10×1
16 17 18 19 20 21 22 23 24 25
function [minval,minIndex]=funcMink(X,I)
[minval{1},j]=mink(X,10);
minIndex={I(j)};
end
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...
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.

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Asked:

on 23 Feb 2023

Commented:

on 23 Feb 2023

Community Treasure Hunt

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

Start Hunting!