필터 지우기
필터 지우기

How to use "if statements" when "ismember" is based on multiple conditions?

조회 수: 37 (최근 30일)
I have two vectors:
indexM = csvread('indexM.csv'); %228 obs
indexI = csvread('indexI.csv'); %199 obs
indexM is a vector where every row indicates a row index for variables at monthly frequency from 2002 to 2020, while indexI is a vector where every row denotes a row index for variables at an irregular frequency from 2002 to 2020.
We can see this by doing:
% monthly dates
tM = datevec(datetime(indexM, 'ConvertFrom', 'datenum'));
tM(:,1) = vertcat(repmat(2002,1,12)', repmat(2003,1,12)', repmat(2004,1,12)', repmat(2005,1,12)', repmat(2006,1,12)', repmat(2007,1,12)', repmat(2008,1,12)', repmat(2009,1,12)', repmat(2010,1,12)', repmat(2011,1,12)', repmat(2012,1,12)', repmat(2013,1,12)', repmat(2014,1,12)', repmat(2015,1,12)',repmat(2016,1,12)',repmat(2017,1,12)',repmat(2018,1,12)',repmat(2019,1,12)',repmat(2020,1,12)');
tM= tM(:,1:3);
tM= datetime(tM);
% irregulare dates
tI = datevec(datetime(indexI, 'ConvertFrom', 'datenum'));
tI(:,1) = vertcat(repmat(2002,1,11)', repmat(2003,1,12)', repmat(2004,1,11)', repmat(2005,1,11)', repmat(2006,1,11)', repmat(2007,1,11)', repmat(2008,1,12)', repmat(2009,1,12)', repmat(2010,1,12)', repmat(2011,1,12)', repmat(2012,1,12)', repmat(2013,1,12)', repmat(2014,1,12)', repmat(2015,1,8)',repmat(2016,1,8)',repmat(2017,1,8)',repmat(2018,1,8)',repmat(2019,1,8)',repmat(2020,1,8)');
tI= tI(:,1:3);
tI = datetime(tI);
Now, what I want to do is writing a loop that says: whenever tI and tM have the same month and year, indexM equals indexI. I wrote this loop as follows:
rr = [];
for i = 1:length(indexM)
if ismember(month(tM(i)) & year(tM(i)), month(tI) & year(tI)) == 1 % if A is found in B
rr(i) = indexM(i);
else
rr(i) = NaN;
end
end
However, what I got back rr is simply indexM. This happens since ismember doesn't find any zero values and this is weird. In fact, for example:
% tI(8) : 11-Sep-2002
% tM(8) : 30-Aug-2002
% which would have meant filling the 8th row of rr with Nan
What am I doing wrong?
Thanks!

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 2월 7일
Your syntax is not correct. Split your condition into 2 separate uses of ismember. Also, ismember returns a value for every element of the second input. Use any to return the single logical result an if statement needs. Since the result is logical, you do not have to compare the result to 1.
I think something like this is what you want (untested)
if any(ismember(month(tM(i)), month(tI))) & any(ismember(year(tM(i)),year(tI)))
  댓글 수: 5
Cris LaPierre
Cris LaPierre 2021년 2월 7일
편집: Cris LaPierre 2021년 2월 7일
Ok, I did a little poking around. I think you want something like this. I've simplified your code a little as well.
indexM = readmatrix('indexM.csv'); %228 obs
indexI = readmatrix('indexI.csv'); %199 obs
% monthly dates
tM = datetime(2002,1,0)+caldays(indexM);
tI = datetime(2002,1,0)+caldays(indexI);
% Change day to be the same for all dates for easier comparison
tM.Day=1;
tI.Day=1;
rr=indexM;
% Change to NaN any tM dates that are not in tI
rr(~ismember(tM,tI))=NaN
rr = 228×1
31 59 90 120 151 181 212 NaN 273 304
Armando MAROZZI
Armando MAROZZI 2021년 2월 7일
thanks a lot! This is exactly what I needed. Thanks again!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by