why the while loop with two conditions don't verify the second condition ?
Mostra commenti meno recenti
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 Commento
madhan ravi
il 11 Apr 2020
Revived from spam.
Risposte (1)
Cris LaPierre
il 11 Apr 2020
Modificato: Cris LaPierre
il 11 Apr 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 Commenti
Cris LaPierre
il 11 Apr 2020
Ah, then change your condition to or (||).
Torsten
il 11 Apr 2020
Then you must use || instead of &&
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!