# How can I save all of the outputs in a loop

2 Ansichten (letzte 30 Tage)
Robert am 25 Feb. 2015
Beantwortet: Guillaume am 25 Feb. 2015
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Guillaume am 25 Feb. 2015
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?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Model Building and Assessment finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by