why the while loop with two conditions don't verify the second condition ?

3 views (last 30 days)
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

Answers (1)

Cris LaPierre
Cris LaPierre on 11 Apr 2020
Edited: Cris LaPierre on 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 Comments

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!