why the while loop with two conditions don't verify the second condition ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
aff=zeros(6,4);
s1=sum(aff(1:6,1:4),1);
s2=sum(aff(1:6,1:4),2);
sol_aff=[6 2 2 2];
m=zeros(6,1);
for i=1:6
m(i)=2;
end
p1=1;
p2=1;
while (p1 ~= 0 ) && (p2 ~= 0)
aff=randi([0 1],6,4);
s1=sum(aff,1);
s2=sum(aff,2);
p1=sum(abs(s1-sol_aff)) ;
p2=sum(abs(m-s2));
end
Respuestas (1)
Cris LaPierre
el 11 de Abr. de 2020
Editada: Cris LaPierre
el 11 de Abr. de 2020
What is it not doing that it should be doing?
After simplying your code, the first time I ran it, it stopped when p2==0 (the second condition of your while loop).
aff=zeros(6,4);
sol_aff=[6 2 2 2];
m=2*ones(6,1);
p1=1;
p2=1;
while p1 ~= 0 && p2 ~= 0
aff=randi([0 1],6,4);
s1=sum(aff,1);
s2=sum(aff,2);
p1=sum(abs(s1-sol_aff))
p2=sum(abs(m-s2))
end
Since I left the semicolon off for p1 and p2, here is a same of the results of each loop.
p1 = 8
p2 = 4
p1 = 7
p2 = 7
...
p1 = 4
p2 = 4
p1 = 8
p2 = 0
3 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!