Go back to initializing values using for loop when condition is not met

I have a for loop where I initiialize random values to variables and I check for certain condition, if my condition is not met I have to reinitialize the values for the same iteration. How do I do so?
Here's a sample
n = 100;
for i = 1:n
x(i) = rand
y(i) = rand
if(x(i)< 0 || y(i)<0)
disp(' I need to reinitialize values for the same i and my n count should be 100')
end
end
So I have to run the loop until I reach my 'n' value and meet my conditions within the loop. Is there a way to do it or should I use different loop like while?

 Akzeptierte Antwort

infinity
infinity am 10 Jul. 2019
Bearbeitet: infinity am 10 Jul. 2019
Hello,
There are several ways, one approach that you can use as follows
clear
n = 100;
x = zeros(1,n);
y = zeros(1,n);
for i = 1:n
xt = rand;
yt = rand;
while (xt*yt<0)
xt = rand;
yt = rand;
end
x(i) = xt;
y(i) = yt;
end

6 Kommentare

In this case, there are chances that "xt = rand and yt = rand " inside while loop could still be less than zero right since these are random variables
while (xt*yt<0)
xt = rand;
yt = rand;
end
Hello,
The meaning of "while loop" with condition xt*yt < 0 is that
if xt*yt < 0 , i.e., xt < 0 or yt < 0 then xt and yt will be initialized untill the condition xt*yt < 0 is violated. Therefore, what you will get after the while loop always garantee that xt*yt >= 0, i.e., xt >= 0 and yt >= 0.
Thank you so much! I understood the working of while loop.
I have one more question too. Since I am a beginner, I would like to know why do we have to use * operator instead of || operator. It means when one value is less than zero the product automatically becomes less than zero? Am I right?
And what should I do if I have to check two conditions say I have to new variables x2t and y2t which depensds on xt and yt are always between (0 and 1) in this code given below
Is my code right?
for i = 1:n
a = 0;
b= 1;
x1(i) = a + (b-a)*rand;
y1(i) = a + (b-a)*rand;
theta = 2*pi*rand;
l = 0.2;
x2(i) = x1(i) + li(i)*cos(theta);
y2(i) = y1(i) + li(i)*sin(theta);
while (x2(i)*y2(i)<0 && x2(i)*y2(i)>1)
x2(i) = x1(i) + li(i)*cos(theta);
y2(i) = y1(i) + li(i)*sin(theta);
x2(i) = x1(i) + li(i)*cos(theta);
y2(i) = y1(i) + li(i)*sin(theta);
end
Hello,
"I would like to know why do we have to use * operator instead of || operator. It means when one value is less than zero the product automatically becomes less than zero? Am I right? "
I can use * operator is because it is equivalent with your condition x < 0 || y < 0. If one of them (x and y) less than 0 then x * y < 0 and inverse.
Also, as you can see that x*y < 0 is shorter than (x< 0 || y < 0). But, of course, you can use your original condition.
"And what should I do if I have to check two conditions say I have to new variables x2t and y2t which depensds on xt and yt are always between (0 and 1) in this code given below
Is my code right?"
Actually, what I have done before are not necessary since function "rand" generate a real random number between 0 and 1. Therefore, now you don't need to use condition to on xt and yt.
But for your current problem, x2 and y2 can be negative and can be greater than 1. Therefore, you should implement conditions and the code could be like
a = 0;
b= 1;
l = 0.2;
for i = 1:n
x1(i) = a + (b-a)*rand;
y1(i) = a + (b-a)*rand;
thetax = 2*pi*rand;
thetay = 2*pi*rand;
x2next = x1(i) + li(i)*cos(thetax);
y2next = y1(i) + li(i)*sin(thetay);
while (x2next < 0 || x2next > 1)
thetax = 2*pi*rand;
x2next = x1(i) + li(i)*cos(thetax);
end
while (y2next < 0 || y2next > 1)
thetay = 2*pi*rand;
y2next = y1(i) + li(i)*sin(thetay);
end
x2(i) = x2next;
y2(i) = y2next;
end
I have put a, b and l out side the loop since they are constants.
More details of function "rand" can be found by typing
help rand
YES ! I did this already. That was my original code. But since x2 is( X2 = x1+ some value ) it might exceed value 1 or become less than 0. So I have to check for the x2 and y2 everytime so that they are between 0-1 and initialize x1 and y1 again for the same iteration until I make sure x2 and y2 are within (0-1)
Hello,
I got your point. You can see my comment above, which is just modified.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by