Calculate slope of groups of numbers in a table
Hi,
I have a table (data_all) with 4 columns.
Column 1 = identifies the group (identity)
Column 2 = time
Column 3 = x values
Column 4 = y values
I need to calculate the slopes of lines created by variables x and y within the groups defined by column 1.
I have been trying to use ‘polyfit’ with ‘varfun’ and ‘splitapply’. I can’t figure how to input 2 variables for a ‘varfun’ function. And the ‘splitapply’ gives an error of ‘unable to perform assignment because the indices on the left side are not compatible with the size of the right side.’
Codes I’ve been trying are below. I would appreciate any help with this!
Trying ‘varfun’ with mean which works but how do you use polyfit and 2 inputs?
data = ["x", "y"];
all_means = varfun(@mean, …
data, …
"InputVariables",data_all, …
"GroupingVariables","identity");
writetable(all_means,’all_means.xlsx’);
Trying ‘findgroups’ with ‘splitapply’. This gives an error of ‘unable to perform assignment because the indices on the left side are not compatible with the size of the right side.’
G = findgroups(data_all.identity)
%%
x = data_all.x
y = data_all.y
n = data_all.n %column of 1s
[slope, intercept] = splitapply(@polyfit,x,y,n,G);Hi,
I have a table (data_all) with 4 columns.
Column 1 = identifies the group (identity)
Column 2 = time
Column 3 = x values
Column 4 = y values
I need to calculate the slopes of lines created by variables x and y within the groups defined by column 1.
I have been trying to use ‘polyfit’ with ‘varfun’ and ‘splitapply’. I can’t figure how to input 2 variables for a ‘varfun’ function. And the ‘splitapply’ gives an error of ‘unable to perform assignment because the indices on the left side are not compatible with the size of the right side.’
Codes I’ve been trying are below. I would appreciate any help with this!
Trying ‘varfun’ with mean which works but how do you use polyfit and 2 inputs?
data = ["x", "y"];
all_means = varfun(@mean, …
data, …
"InputVariables",data_all, …
"GroupingVariables","identity");
writetable(all_means,’all_means.xlsx’);
Trying ‘findgroups’ with ‘splitapply’. This gives an error of ‘unable to perform assignment because the indices on the left side are not compatible with the size of the right side.’
G = findgroups(data_all.identity)
%%
x = data_all.x
y = data_all.y
n = data_all.n %column of 1s
[slope, intercept] = splitapply(@polyfit,x,y,n,G); Hi,
I have a table (data_all) with 4 columns.
Column 1 = identifies the group (identity)
Column 2 = time
Column 3 = x values
Column 4 = y values
I need to calculate the slopes of lines created by variables x and y within the groups defined by column 1.
I have been trying to use ‘polyfit’ with ‘varfun’ and ‘splitapply’. I can’t figure how to input 2 variables for a ‘varfun’ function. And the ‘splitapply’ gives an error of ‘unable to perform assignment because the indices on the left side are not compatible with the size of the right side.’
Codes I’ve been trying are below. I would appreciate any help with this!
Trying ‘varfun’ with mean which works but how do you use polyfit and 2 inputs?
data = ["x", "y"];
all_means = varfun(@mean, …
data, …
"InputVariables",data_all, …
"GroupingVariables","identity");
writetable(all_means,’all_means.xlsx’);
Trying ‘findgroups’ with ‘splitapply’. This gives an error of ‘unable to perform assignment because the indices on the left side are not compatible with the size of the right side.’
G = findgroups(data_all.identity)
%%
x = data_all.x
y = data_all.y
n = data_all.n %column of 1s
[slope, intercept] = splitapply(@polyfit,x,y,n,G); varfun, splitapply, polyfit MATLAB Answers — New Questions