How to use if statement for than one variables?
Mostrar comentarios más antiguos
I would like to set one if statement. I am using the following code:
filename2= 'OutputFile1L.xlsx';
[d2,tex]= xlsread(filename2);
a=d2(:,1);
for ii=1:numel(a)
if a(ii)==0
b(ii)==1 & c(ii)==0
elseif a(ii)==1
b(ii)==0 & c(ii)==0
elseif a==2
b(ii)==0 & c(ii)==0
else
b(ii)==nan & c(ii)==nan
end
but command window shows me
Undefined function or variable b
What is the wrong? could you please help me?
Respuesta aceptada
Más respuestas (2)
Yongjian Feng
el 4 de Jul. de 2021
You meant this?
if a(ii)==0
b(ii)=1;
c(ii)=0;
elseif a(ii)==1
b(ii)=0;
c(ii)=0;
elseif a==2
b(ii)=0;
c(ii)=0;
else
b(ii)=nan;
c(ii)=nan;
end
1 comentario
Sulaymon Eshkabilov
el 4 de Jul. de 2021
Editada: Sulaymon Eshkabilov
el 4 de Jul. de 2021
There are a couple of ERRs in Feng's proposed code:
for ... % is missing
...
if ..
elseif a==2 % MUST be a(ii)==2
...
end
end
Sulaymon Eshkabilov
el 4 de Jul. de 2021
Editada: Sulaymon Eshkabilov
el 4 de Jul. de 2021
There are several errs in your loop code that is not advised. Just because it is slow and inefficient.
Here is an easy sol isntead of loop based code:
...
a=d2(:,1);
Idx0=find(a(:)==0);
Idx1=find(a(:)==1 | a(:)==2);
Idx2=find(a~=0 & a~=1 & a~=2);
b(Idx0)=1; c(Idx0)=0;
b(Idx1)=0; c(Idx1)=1;
b(Idx2)=nan; c(Idx2)=nan;
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!