Storing regression coefficients in a loop

8 views (last 30 days)
Hello,
I'm doing a simple linear regression on a 20x30x365 matrix (latitude, longitude, time). I want to store the slope and p - value for each lat/lon pair such that the end result are two 20x30 matrices. I'm attaching some dummy data and my attempted code below. Thank you.
X = size(prec,3);
i = 20;
j = 30;
slopes = zeros(i,j);
pvals = zeros(i,j);
for i = 1:i
for j= 1:j
mdl = fitlm(X,prec(i,j,:));
slope = table2array(mdl.Coefficients(2,1));
pval = table2array(mdl.Coefficients(2,4));
slopes = slope(i,j);
pvals = pval(i,j);
end
end

Accepted Answer

VBBV
VBBV on 7 Dec 2021
Edited: VBBV on 8 Dec 2021
slopes(i,j) = slope;
pvals(i,j) = pval;
  2 Comments
Elin Jacobs
Elin Jacobs on 8 Dec 2021
My bad, this works great! thank you!!

Sign in to comment.

More Answers (0)

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by