If and elseif with Or
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
KL
el 3 de Nov. de 2017
Editada: KL
el 3 de Nov. de 2017
when you use | |, you should use it like,
if year == 2012 || year == 2016 % and so on
you need have "an expression" on both side of the | |.
when you write
if year == 2012 || 2016
it checks the first part of the condition (year==2012) but the second part is simply a number which is not zero, so the condition becomes true. So based on what you wrote, the condition becomes always true.
Más respuestas (0)
Ver también
Categorías
Más información sobre Language Fundamentals en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!