필터 지우기
필터 지우기

2D Random path within a unit square

조회 수: 5 (최근 30일)
KalMandy
KalMandy 2017년 5월 21일
댓글: Image Analyst 2017년 5월 22일
Hi all, I want to generate a random path within a unit square and get the x,y coordinates of n=1000 points on it. start point and the end point can be picked up from the vertices of the unit square, i.e, (0,0), (0,1),(1,0),(1,1).
The code I have up to now is given as follows. But it does not restrict the path to lie inside the unit square. How can I solve this?
n=1000;
mypath = rand(n,2);
xp=mypath(:,1);
xp_s=cumsum(xp);
yp=mypath(:,2);
yp_s=cumsum(yp);
plot(xp_s,yp_s)

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2017년 5월 21일
편집: Andrei Bobrov 2017년 5월 21일
n=1000;
mypath = rand(n,2);
m = cumsum(mypath);
mout = m./m(end,:); % MATLAB >= R2016b
mout = bsxfun(@rdivide,m,m(end,:)); % MATLAB < R2016b

Image Analyst
Image Analyst 2017년 5월 21일
I have a number of random walk demos and I've attached them. Feel free to use them however you wish.
  댓글 수: 2
KalMandy
KalMandy 2017년 5월 21일
sorry, I could not find the solution with this.
Image Analyst
Image Analyst 2017년 5월 22일
In your loop, simply don't add the next random step if it would take you outside the unit box. Just get another step instead.

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by