How to run if and exist ?

조회 수: 9 (최근 30일)
Luca
Luca 2021년 5월 9일
댓글: Luca 2021년 5월 15일
Hi,
I'm trying to run this loop.
for year= 1962:2016
for id=10000:93436
sub_regress = subsetPCA(subsetPCA.ID ==id & subsetPCA.years==year,:);
if size(sub_regress,1) ~= 0
[~, IDPCA1, yearsPCA1]= findgroups(sub_regress.ID, sub_regress.years);
[~, IDPCAd, datesPCA1] = findgroups(sub_regress.ID, sub_regress.dates);
modeltest = fitlm(sub_regress, 'RET~PC1+PC2+PC3+PC4+PC5');
RES_PCA = modeltest.Residuals.Raw;
RES_PCAtbl= table(IDPCAd,datesPCA1, RES_PCA);
RES_PCAtbl.Properties.VariableNames([1 2]) = {'ID' 'dates'};
if exist final_residuals
final_residuals =[final_residuals; RES_PCA];
else
final_residuals = RES_PCA;
end
end
end
end
For some reason it is not acepting final_residual in the if exist function with if statement? Does someone know how what the problem is?
Thank you in advance
  댓글 수: 1
Jonas
Jonas 2021년 5월 9일
try running it using bracket like
a=5;
exist('a','var')

댓글을 달려면 로그인하십시오.

채택된 답변

VBBV
VBBV 2021년 5월 9일
편집: VBBV 2021년 5월 9일
final_residuals = []
for year= 1962:2016
for id=10000:93436
sub_regress = subsetPCA(subsetPCA.ID ==id & subsetPCA.years==year,:);
if size(sub_regress,1) ~= 0
[~, IDPCA1, yearsPCA1]= findgroups(sub_regress.ID, sub_regress.years);
[~, IDPCAd, datesPCA1] = findgroups(sub_regress.ID, sub_regress.dates);
modeltest = fitlm(sub_regress, 'RET~PC1+PC2+PC3+PC4+PC5');
RES_PCA = modeltest.Residuals.Raw;
RES_PCAtbl= table(IDPCAd,datesPCA1, RES_PCA);
RES_PCAtbl.Properties.VariableNames([1 2]) = {'ID' 'dates'};
A = exist('final_residuals')
if A
final_residuals =[final_residuals; RES_PCA];
else
final_residuals = RES_PCA;
end
end
end
end
  댓글 수: 1
Luca
Luca 2021년 5월 15일
Thank you it worked

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by