필터 지우기
필터 지우기

If and continue conditions

조회 수: 3 (최근 30일)
Anton Sørensen
Anton Sørensen 2020년 4월 10일
댓글: Anton Sørensen 2020년 4월 10일
Hi All,
I want Matlab to provide two matrices of same length (1*25). So basically I want the loop to only run an iteration if both conditions are satisfied.
If any of T6 or T7 is shorter that 6 observations, it should skip that particular iteration for both "estimates_2010" and "estimates_2011".
I have succeeded in previous loops but different variables with this procedure, however in this loop I am ending up with a 1*25 matrix for "alpha_2010" (which I should), but ending up with af 1*23 matrix for "alpha_2011" (which is wrong). T6, T7, Carhart_t6 and Carhart_t7 is of same length and are cell variables, lags is a fixed number on 2 which is necessary in the input function "nwest". I was certain, that the "|" was an or condition, so both variable condition needed to be satisfied in order to proceed.
I am trying to do the following:
for i = 1:size(Afkast,2) % Afkast is a 12*25 matrix
if length(T6{i}) <6 | length(T7{i}) <6 % I want my if condition to evaluate both variables, if either is shorter than 6, it should be skipped.
continue
end
estimates_2010 = nwest(T6{i},Carhart_t6{i},lags); estimates_2011 = nwest(T7{i},Carhart_t7{i},lags);
alpha_2010(:,i) =estimates_2010.beta(1); alpha_2011(:,i) = estimates_2011.beta(1);
end
I have done it for multiple different variables with same conditions, however there seem to be something wrong, maybe I am misunderstanding the "|" operator.

채택된 답변

Rik
Rik 2020년 4월 10일
If you use the debugger to step through your code line by line, you will have notice that your condition works (although I would suggest ||, because that skips the evaluation of the second part if the first is true).
The actual issue is with code like this:
alpha_2010(:,i) =estimates_2010.beta(1);
If you skipped one iteration because the lengths are incorrect, a line like this will fill the skipped line with zeros, unless you pre-allocated it before your loop:
k= ___ ;
alpha_2010 = NaN(k,size(Afkast,2));
for i = 1:size(Afkast,2)
You could also use a separate column counter to fill you columns consecutively.
  댓글 수: 8
Rik
Rik 2020년 4월 10일
The code below is much cleaner and easier to adapt to what you need than your original code. Whenever you find yourself using numbered variables you should consider using an array instead. You should also avoid long lines of code (keeping it under 100 chars wide improves readability). Another good practice is to write your comments in English so people you share your code with don't need to use Google translate.
The code below is a complete reproduction of what your Persistance script is doing. No need for clc,clear all, close all, as you don't print to the command window, or open any figures. clear all should be replaced by clear, for reasons the documentation explains.
%% Persistence tests foretages med udgangspunkt i alle geografiske områder
% Bemærk persistence foretages kun for Carharts 4-factor
S=load('aDanmark.mat');
aDanmark=table2array(S.aDanmark(:,2:end))';
S=load('bDanmark.mat');
bDanmark=table2array(S.Danmark(:,2:end));
rf = bDanmark(:,5); % Er den samme for alle foreninger
Afkast = aDanmark - rf; %Variablen gøres universel, så de forskellige investeringsuniverser bare skal loades ind respektivt, så koden vil være den samme.
mkt_d=+bDanmark(:,1)-rf; %Risikojusterede markedsafkast
alpha=ones(size(mkt_d(:,1))); %Der inkluderes intercept
carhart=[ alpha mkt_d (bDanmark(:,2:4))]; %De fire faktorer i Carharts model medtages.
lags = floor(4*(12/100).^(2/9)); % Da der for hver regression kun anvendes 12 observationer, bruges Newey-West (1994) plug-in procedure
N_years=size(Afkast,1)/12;
columns=size(Afkast,2);
Carhart_t=cell(N_years,columns);
T=cell(N_years,columns);
alpha=zeros(N_years,columns);
%generate the Carhart_t and T arrays
for i = 1:columns
for year=1:N_years
months=(1:12)+12*(year-1);%1:12 for year 1, 13:24 for year 2 etc
months=months(isnan(Afkast(months,i))==0);
% Hver periode sættes op, så foreningerne og dertilhørende faktorer matcher hinanden for hver periode
Carhart_t{year,i}=carhart(months,:);
T{year,i}=Afkast(months,i);
end
end
%calculate the alpha array
%%%replaces alpha_2005, alpha_2006, etc
for year=1:(N_years-1)
for i=1:columns
if numel(T{year,i}) <6 || numel(T{year+1,i}) <6 % Minimumskrav til antal obs for hver periode, er der i en af perioderne færre end 6 obs, kan foreningen ikke medtages
%mark alpha with NaN
alpha(year ,i)=NaN;
alpha(year+1,i)=NaN;
else
estimates_year = nwest(T{year ,i},Carhart_t{year ,i},lags);
estimates_year_p_1 = nwest(T{year+1,i},Carhart_t{year+1,i},lags);
alpha(year ,i)=estimates_year.beta(1);
alpha(year+1,i)=estimates_year_p_1.beta(1);
end
end
end
Anton Sørensen
Anton Sørensen 2020년 4월 10일
Much cleaner!
Thanks Rik, I think this will help me alot, since I need to do the same for 9 more variables.
Have a great easter!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by