MATLAB Answers

0

while loops not work using randsample

Asked by maryhan mohamed on 7 Nov 2019 at 21:52
Latest activity Commented on by maryhan mohamed on 11 Nov 2019 at 12:28
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

  0 Comments

Sign in to comment.

2 Answers

Answer by the cyclist
on 7 Nov 2019 at 21:59
 Accepted Answer

The specific reason is that you never enter the first while loop, because the condition
eq1<1 && eq2>1
is never true. I didn't trace it back further than that.
I recommend using the debugger.

  3 Comments

Thanks for the reply, this is an example for what i need to do
eq1= [1.2 0.5 1.2];
eq2=[0.5 1.2 0.5];
while (eq1>1)&&(eq2<1)
eq22=(eq2+0.5);
eq11=(eq1-0.5);
while(eq2>1)&&(eq1<1)
eq22=(eq2-0.5);
eq11=(eq1+0.5);
end
end
I want when eq1= 1.2 and eq2 = 0.5 then eq22=1 , eq11=0.7
and when eq2=1.2 and eq1=0.5 then eq22=0.7, eq11=1
so what should be the right while loop code?
the cyclist
on 10 Nov 2019 at 14:25
This is going to sound a little bit harsh, but it looks like you are taking the English meanings of the MATLAB keywords, and using them in a way that you hope will do what you want. That's not how programming languages work. You need to understand the syntax of each command, and know what it will do at each step.
OK, after those harsh words, here is some code that I believe does what you are asking. I have added comments to make it as clear as I can what each step is doing. I have also organized the code more neatly, which is surprisingly helpful in seeing what the entire algorithm is doing.
I tried maintain what I think is the way you were approaching the problem, and coding it in a way that will make sense to you.
% Inputs
eq1 = [1.2 0.5 1.2];
eq2 = [0.5 1.2 0.5];
% Get the length of the input vectors. We'll do a FOR loop over each
% element, so we need to know how many iterations in the FOR loop.
L = length(eq1);
% Initialize the values of eq11 and eq22 to zero. That will be the default
% value, if none of the IF conditions are met
eq11 = zeros(1,L);
eq22 = zeros(1,L);
% We need to test the condition for each pair of elements, so loop over
% each pair
for ii = 1:L
% Test the first condition, and fill in the values if it is met
if eq1(ii)>1 && eq2(ii)<1
eq22(ii) = eq2(ii) + 0.5;
eq11(ii) = eq1(ii) - 0.5;
end
% Test the second condition, and fill in the values if it is met
if eq2(ii)>1 && eq1(ii)<1
eq22(ii) = eq2(ii) - 0.5;
eq11(ii) = eq1(ii) + 0.5;
end
end
I'd like to emphasize that in MATLAB, there are much more elegant and efficient ways of doing this calculation. (It could be done in just a line or two, without using a for loop at all.) But I thought that might be more confusing, given your level of understanding of the language.
I would strongly recommend taking advantage of the many free resources available to learn MATLAB basics. One good starting point is the MATLAB Onramp.
Thanks so much for your help

Sign in to comment.


Answer by Guillaume
on 7 Nov 2019 at 21:59

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

  0 Comments

Sign in to comment.