How can I save all of the outputs in a loop

1 回表示 (過去 30 日間)
Robert
Robert 2015 年 2 月 25 日
回答済み: Guillaume 2015 年 2 月 25 日
Below is my code (excluding loading the data). It runs about 1500 stepwise regressions using a for loop. I need to ad something to the code so the results of each stepwise regression will be saved as they loop through. Any help greatly appreciated I'm a newbie to matlab.
%Sort the Data
[rows, cols]=size(A03);
for i = 1:rows
for j = 1:cols
y=zeros(8,1)
x=zeros(8,3)
for m=1:8
y(m)=NDVI((m-1)*rows+i,j)
x(m,1)=PET((m-1)*rows+i,j)
x(m,2)=P((m-1)*rows+i,j)
x(m,3)=Amp((m-1)*rows+i,j)
end
%Run the stepwise Regression
if sum(sum(isnan(x)),2)==0 && sum(isnan(y))==0
xx=x(:,1:3); yy=y(:,1);
[B,SE,PVAL,INMODEL,STATS,NEXTSTEP,HISTORY]=stepwisefit(xx,yy,'penter',.05);
end
end
end

回答 (1 件)

Guillaume
Guillaume 2015 年 2 月 25 日
The simplest way is to save your results in matrices:
B = zeros([size(A03), 3); %third dimension is the number of columns in xx
SE = zeros([size(A03), 3);
PVAL = zeros([size(A03), 3);
INMODEL = logical(zeros([size(A03), 3));
STATS = struct('source', cell(size(A03))); %other fields automatically created on first iteration
NEXTSTEP = zeros(size(A03));
HISTORY = struct('B', cell(size(A03))); %other fields automatically created on first iteration
for row = %... %note don't use i and j as variables
%...
[B(row, col, :), SE(row, col, :), PVAL(row, col, :), INMODEL(row, col, :), STATS(row, col), NEXTSTEP(row, col), HISTORY(row, col)] = stepwisefit(...);
%...
Do you actually need all the return values of stepwisefit?

カテゴリ

Help Center および File ExchangeGaussian Process Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by