Splitapply: Returning multiple output values
显示 更早的评论
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
采纳的回答
更多回答(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)
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!