I have two date vectors and one vector of numbers:
% vector 1
t1 = datetime(2002,01,31):calmonths:datetime(2020,12,31);
t1= t1';
% vector 2
t2 = datetime(2002,01,23):calmonths:datetime(2020,12,23);
t2 = t2';
t2(randsample(100,28))=[];
% vector of numbers
r = randn(228,1);
What I want to do is, first, to compare t1 and t2 to see when they are equal on the basis of months and years and, then, keep only the rows of r when this happens. What I tried was:
% first solution
rr = [];
for i = 1:length(r)
if ismember(month(t1(i)) & year(t1(i)), month(t2) & year(t2)) == 1
rr(i) = r(i);
else
rr(i) = NaN;
end
end
% second solution
for i = 1:length(r)
if any(ismember(month(t1(i)), month(t2))) & any(ismember(year(t1(i)),year(t2)))
rr(i) = r(i);
else
rr(i) = NaN;
end
end
In either case, it desn't work. It always gives me back 1 even if there there is no match.
Can anyone help me?
Thanks!
Aucun commentaire:
Enregistrer un commentaire