Splitapply: Returning multiple output values
Show older comments
Hello everyone. I am currently stuck at a problem which refers to the application of the splitapply function for a function with multiple output values. More specifically, i am trying to get the correlation coefficient r, as well as the corresponding p-value for the subsets of multiple columns of a table, specified by group numbers. Here is an example:
A = array2table(rand(100,1));
A.Properties.VariableNames{1} = 'Row1';
A.Row2 = rand(100,1);
B = [1:1:10]';
A.Groups = repmat(B,10,1);
Given is a table A with 2 columns, containing 10 observations in chronological order for 10 different groups. The following formula returns the r coefficients i am looking for.
R = splitapply(@(a,b) {corrcoef(a,b)},A.Row1,A.Row2,A.Groups);
But the syntax for multiple outputvalues, [R,P] = corrcoef (a,b), does not work if i simply put it in front of the splitapply funcion as follows:
[R,P] = splitapply(@(a,b) {corrcoef(a,b)},A.Row1,A.Row2,A.Groups);
I guess i have to use a local function to recieve both of the values. Unfortunately i am not really experienced in using them, and after doing a little research, i have still absoluty no idea what to type. It would be a blessing if somebody could help me out.
Thank you very much for reading
Accepted Answer
More Answers (1)
Use arrayfun instead.
A = array2table(rand(100,1));
A.Properties.VariableNames{1} = 'Row1';
A.Row2 = rand(100,1);
B = [1:1:10]';
A.Groups = repmat(B,10,1);
[R,P] = arrayfun(@(i) corrcoef(A.Row1(A.Groups==i), A.Row2(A.Groups==i)), ...
unique(A.Groups), 'UniformOutput',false)
1 Comment
Maximilian Hauschel
on 25 Jun 2021
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!