I'm using a for loop to create a new random variable X with range [0 1], I want the forloop to ignore values above 1 and iterate untill all the values are in range.

조회 수: 4 (최근 30일)
clc; clear all; close all;
n = 200; % Number of iteration
ro = 0.75; % Correlation factor
X = zeros(1,n);
x = zeros(1,n);
for ii = 1:n
x = rand(1,ii);
y = rand(1,ii);
X = x*ro+y*sqrt(1-ro^2);
if X > 1 % If X > 1 start iteration no. ii again until X is below 1
ii = ii - 1;
end
end
plot(x,X,'+')

채택된 답변

Niels
Niels 2015년 2월 25일
편집: Niels 2015년 2월 25일
Change your for-loop to a while-loop if you want to keep looping. I also believe that you stored your data incorrectly, as x, y and X were not stored at every time step, while your random number generation grows larger for every iteration ii...
clc; clear all; close all;
n = 200; % Number of iteration
ro = 0.75; % Correlation factor
X = zeros(1,n);
x = zeros(1,n);
y = zeros(1,n);
ii = 1;
while (ii <= n)
x(ii) = rand();
y(ii) = rand();
X(ii) = x(ii)*ro+y(ii)*sqrt(1-ro^2);
if (X(ii) <= 1) % Don't increment if X > 1.
ii = ii + 1;
end
end
figure, plot(x,X,'+')

추가 답변 (3개)

Joseph Cheng
Joseph Cheng 2015년 2월 24일
you'd want to use "continue" to skip the iteration.
for ind =1:5
if ind==3
continue
else
disp(ind)
end
  댓글 수: 1
Fredrik Slungård
Fredrik Slungård 2015년 2월 25일
편집: Fredrik Slungård 2015년 2월 25일
Sorry for taking so long answering, as I'm relative new to Matlab I've never used the continue function. But I don't want to skip the iteration, only redo iteration no. i if X > 1, and I still get about 30 values where X > 1.
clc; clear all; close all;
n = 200;
ro = 0.75;
X = zeros(1,n);
x = zeros(1,n);
for ii = 1:n
x = rand(1,ii);
y = rand(1,ii);
X = x*ro+y*sqrt(1-ro^2);
if X > 1;
continue
else
disp(X)
end
end
plot(x,X,'+')

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2015년 2월 24일
Why not just only generate random values that meet your criteria? Here's an example:
% Solve for x
syms x ro y
X = x*ro+y*sqrt(1-ro^2);
xx = solve(X,x)
% Plug in values
yval = -0.1;
roval = 0.3;
xmax = double(subs(xx,{y, ro},{yval,roval}));
% Generate only random numbers that meet criteria
xrand = rand(100,1)./xmax;
% Create X
Xfinal = xrand.*roval+yval*sqrt(1-roval^2);
% Check all X < 1
assert(all(Xfinal < 1))
  댓글 수: 1
Fredrik Slungård
Fredrik Slungård 2015년 2월 25일
Thank you for quick answer! As this is only a small part of a greater script, I think it would be better suited using a forloop in this particular case.

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2015년 2월 25일
편집: Sean de Wolski 2015년 2월 25일
I am guessing that what you actually want is the following. It will give you 200 values in X which are all less than or equal to 1.
X = zeros(1,n);
x = zeros(1,n);
for ii = 1:n
b = true;
while b
t1 = rand;
t2 = t1*ro+rand*sqrt(1-ro^2);
b = t2 > 1;
end
X(ii) = t2;
x(ii) = t1;
end
plot(x,X,'+')

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by