필터 지우기
필터 지우기

How can I save all of the outputs in a loop

조회 수: 2 (최근 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 CenterFile Exchange에서 Model Building and Assessment에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by