while loops not work using randsample

1 visualización (últimos 30 días)
maryhan mohamed
maryhan mohamed el 7 de Nov. de 2019
Comentada: maryhan mohamed el 11 de Nov. de 2019
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

Respuesta aceptada

the cyclist
the cyclist el 7 de Nov. de 2019
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 comentarios
the cyclist
the cyclist el 10 de Nov. de 2019
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.
maryhan mohamed
maryhan mohamed el 11 de Nov. de 2019
Thanks so much for your help

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 7 de Nov. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by