while loops not work using randsample
Show older comments
please why this code is not work,array u and w has no values.
wi=10000000;
bwf=1668839.63;
bwm=352103.035;
p=1;
T = [];u=[];w=[];nf2ar=[];nm2ar=[];nf3ar=[];nm3ar=[];
for k=1:5
A= [1 2 3 4 5 6 7];
b = [1 2 3 4 5 6 7];
nf2 = randsample(A,1);
nf2ar=[nf2ar,nf2];
nm2 = randsample (b,1);
nm2ar=[nm2ar,nm2];
eq1=((bwf*nf2)/wi)+((bwm*nm2)/wi);
nf3 = randsample(A,1);
nf3ar=[nf3ar,nf3];
nm3 = randsample (b,1);
nm3ar=[nm3ar,nm3];
eq2=((bwf*nf3)/wi)+((bwm*nm3)/wi);
while (eq1<1 && eq2>1)
eq11=((bwf*nf2)/wi)+((bwm*(nm2+p))/wi);
eq22=((bwf*nf3)/wi)+((bwm*(nm3-p))/wi);
while (eq1>1 && eq2<1)
eq11=((bwf*nf2)/wi)+((bwm*(nm2-p))/wi);
eq22=((bwf*nf3)/wi)+((bwm*(nm3+p))/wi);
u= [u,eq11];
w=[w,eq22];
g=sum(u);
x= g/5;
G=sum(w);
y=G/5;
end
end
end
Accepted Answer
More Answers (1)
Guillaume
on 7 Nov 2019
Maybe if you used proper indenting in your code, you'd see the silliness of it:
%... code irrelevant
while eq1 < 1 && eq2 > 1
%so if we get here we know that eq1 IS SMALLER than 1 AND eq2 IS GREATER than 1
%... stuff that doesn't change eq1 or eq2
while eq > 1 && eq2 < 1 %we know it's NEVER the case otherwise we wouldn't get here
%u and w calculated here
%guaranteed to never happen!
end
end
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!