hello, i have a 5x5 matrix and i am standing at 2,2 location, now from here i have to take one step and jump to new value. i can take jump in all 4 directions i,e i-1,i+1,j-1,j+1 with equal probability of 0.25, and also i have to update my location for making a counter loop for continous movement inside matrix.how can i code it using randperm ? or any other command available ?

댓글 수: 5

Walter Roberson
Walter Roberson 2018년 11월 25일
What should happen if you reach an edge and randomly select to go outside?
randperm() is not going to help you with this. Consider randi()
Image Analyst
Image Analyst 2018년 11월 25일
Is this homework? Just compute the candidate next location. If it's outside of your boundaries, then just get another value, which will most likely be in a different direction and be inside the box.
Muzaffar Habib
Muzaffar Habib 2018년 11월 26일
If i hit the edge i will bounce back to the same location.
Muzaffar Habib
Muzaffar Habib 2018년 11월 26일
Thank You. Much appreciated help. I will try to code that up with randi()
To "bounce back to the same location" you can use techniques such as
new_x_position = max( min(new_x_position, maximum_x_position), minimum_x_position )

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

 채택된 답변

Eyal Ben-Hur
Eyal Ben-Hur 2018년 11월 26일

0 개 추천

Here is a suggestion:
A = zeros(5); % the matrix
p ={2,2}; % initial location
A(p{:}) = 1; % set initial location
N = 20; % no. of steps
for k = 1:N
s = 1-(rand<0.5)*2; % random +/-
vh = randi(2); % random vertical or horizontal
p{vh} = p{vh}+s; % move
p{vh} = abs(p{vh}-5*(p{vh}==6 || p{vh}==0)); % if outbound, return from the other side
A = zeros(5); % remove last location
A(p{:}) = 1; % set new location
end
Since it looks like homwork I leave it to you to understand the details.

댓글 수: 1

Muzaffar Habib
Muzaffar Habib 2018년 11월 27일
Thank U. it did work with some ammendments.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2018년 11월 25일

댓글:

2018년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by