why the while loop with two conditions don't verify the second condition ?
Afficher commentaires plus anciens
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
1 commentaire
madhan ravi
le 11 Avr 2020
Revived from spam.
Réponses (1)
Cris LaPierre
le 11 Avr 2020
Modifié(e) : Cris LaPierre
le 11 Avr 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 commentaires
Cris LaPierre
le 11 Avr 2020
Ah, then change your condition to or (||).
Torsten
le 11 Avr 2020
Then you must use || instead of &&
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!