If and elseif with Or
Ältere Kommentare anzeigen
I would like to distinguish id2 if we encounter a leap year. However, this current code sets id2 to true for values(days) 32 through 60 for nonleap years as well, so the elseif isn't working properly.
function mmean_Feb_year=mmean_Feb_(year,file)
T = readtable(file);
M =table2array(T);
id1 = M(:,1)==year;
dmean_year = M(id1,:);
if year==2008||2012||2016
id2 = dmean_year(:,2)>=32 & dmean_year(:,2)<=60;
elseif year==2007||2009||2010||2011
id2 = dmean_year(:,2)>=32 & dmean_year(:,2)<=59;
end
dmean_Feb_year = dmean_year(id2,:);
mmean_Feb_year = mean(dmean_Feb_year(:,3:end),'omitnan');
end
I also tried the following, but this did not save the leap year id2, and made id2 true for days 32-59 for all years.
function mmean_Feb_year=mmean_Feb_(year,file)
T = readtable(file);
M =table2array(T);
id1 = M(:,1)==year;
dmean_year = M(id1,:);
if year==2008||2012||2016
id2 = dmean_year(:,2)>=32 & dmean_year(:,2)<=60;
end
if year==2007||2009||2010||2011
id2 = dmean_year(:,2)>=32 & dmean_year(:,2)<=59;
end
dmean_Feb_year = dmean_year(id2,:);
mmean_Feb_year = mean(dmean_Feb_year(:,3:end),'omitnan');
end
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Language Fundamentals finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!