code of trace path
2 ビュー (過去 30 日間)
古いコメントを表示
hi, say have this array: x=
1 0 0
0 0 0
0 0 0
i want anyone help me to write algorithm to trace any path begin from (1,1) and have to stop in (3,3), but when passing position such x(i,j),x(i,j) will be =1 , otherwise will be zero where sum(Xij)<=2 if j=1:3 or sum(xij)<=2 if i=1:3.
it is like sequence alignment algorithm, but I do not want use dynamic programming algo. to do this task. I wrote code but did not get the exact result who can help me? Thanks in advance
5 件のコメント
Walter Roberson
2012 年 1 月 23 日
To check: acceptable moves are right, down, diagonal up left, diagonal up right, diagonal down left, diagonal down right, but not plain up, and not plain left?
Is 3x3 the sample size for this question, but the real task will use larger matrice? Or is 3x3 the actual size you will be using?
採用された回答
Walter Roberson
2012 年 1 月 23 日
Your conditions appear to translate as:
- first move can be in any of the three directions (random choice of 3)
- after that, the next move cannot be in the same direction as the one immediately previous (random choice of 2)
- when you reach the bottom or right edge if you do not already happen to be at the bottom right corner, you are exactly one space away and your move is forced and legal.
I have not proven that you cannot intersect the edges except for the target or one space from the target, but my mental modeling is that it is the case for square matrices under those move restrictions.
0 件のコメント
その他の回答 (3 件)
Dr. Seis
2012 年 1 月 23 日
Not entirely clear on an acceptable result, but if this is an example of an acceptable result:
1 1 1
0 0 1
0 0 1
Then this will find a random path for any MxN matrix:
% User Defined %
%%%%%%%%%%%%%%%%
M = 3; % number of rows
N = 3; % number or cols
overlap_allowed = 0; % Allow overlap of positions, 1-Yes, 0-No
% Define possible moves on 3x3 grid below
% U-Up, D-Down, L-Left, R-Right, S-Stay
move_grid = [0, 0, 0;... % [UL, U, UR
0, 0, 1;... % L, S, R
0, 1, 1]; % DL, D, DR]
% Automatically Defined %
%%%%%%%%%%%%%%%%%%%%%%%%%
path = zeros(M,N); path(1) = 1; % Initialize path
move_nums = 1:9;
move_inc = zeros(3,3,2); % Initialize movement increment index
move_inc(:,:,1) = [-1,-1,-1;...
0, 0, 0;...
1, 1, 1];
move_inc(:,:,2) = [-1, 0, 1;...
-1, 0, 1;...
-1, 0, 1];
moves_allowed = ones(M,N,9); % Define moves allowed for each position
moves_allowed(1,:,[1 4 7])=0;
moves_allowed(end,:,[3 6 9])=0;
moves_allowed(:,1,[1 2 3])=0;
moves_allowed(:,end,[7 8 9])=0;
moves_allowed(:,:,~move_grid)=0;
curr_pos = [1,1]; count = 0;
while path(M,N) == 0
temp = reshape(moves_allowed(curr_pos(1),curr_pos(2),:),...
size(move_nums));
poss_move = move_nums(temp==1);
if ~overlap_allowed
temp_poss_move = ones(1,length(poss_move));
for i = 1 : length(poss_move)
if path(curr_pos(1)+move_inc(poss_move(i)),...
curr_pos(2)+move_inc(poss_move(i)+9)) == 1
temp_poss_move(i) = 0;
end
end
poss_move = poss_move(temp_poss_move == 1);
end
rand_move = randperm(length(poss_move));
curr_move = poss_move(rand_move(1));
curr_pos = curr_pos + [move_inc(curr_move),move_inc(curr_move+9)];
path(curr_pos(1),curr_pos(2)) = path(curr_pos(1),curr_pos(2))+1;
count = count + 1;
end
disp(count)
disp(path)
I also included the option for being able to overlap positions, but I assume you did not want to do this for your example. If the example of the acceptable result is, in fact, not acceptable then I can make that modification. Let me know.
4 件のコメント
Dr. Seis
2012 年 1 月 24 日
As a quick "fix" you can simply run my code and check (at the end) to see if the sums of the rows and columns conform to your specifications... if it doesn't conform, then it can simply be re-ran to generate a new random path.
The code above should work for any MxN matrix where M>=2 and N>=2.
huda nawaf
2012 年 1 月 24 日
4 件のコメント
Walter Roberson
2012 年 1 月 24 日
Ah, my mental model of the process was incorrect. Okay, so if you intersect an edge more than 1 unit from the goal, then rerun. I would need to think about whether I could fix the algorithm.
Dr. Seis
2012 年 1 月 24 日
In this version, there is a check that will modify the possible path directions based on whether the row/col it can move to already has reached the maximum number of occupations. However, this check doesn't handle the bottom and right edges properly so there is still the check at the end to make sure that nothing is violated. It should be significantly fast than the previous version for larger matrices.
% User Defined %
%%%%%%%%%%%%%%%%
M = 5; % number of rows
N = 5; % number or cols
overlap_allowed = 0; % Allow overlap of positions, 1-Yes, 0-No
M_allowed = 2; % number of times allowed to occupy a specific column
N_allowed = 2; % number of times allowed to occupy a specific row
% Define possible moves on 3x3 grid
% U-Up, D-Down, L-Left, R-Right, S-Stay
move_grid = [0, 0, 0;... % [UL, U, UR
0, 0, 1;... % L, S, R
0, 1, 1]; % DL, D, DR]
% Automatically Defined %
%%%%%%%%%%%%%%%%%%%%%%%%%
path = zeros(M,N); path(1) = 1; % Initialize path
move_nums = 1:9;
move_inc = zeros(3,3,2); % Initialize movement increment index
move_inc(:,:,1) = [-1,-1,-1;...
0, 0, 0;...
1, 1, 1];
move_inc(:,:,2) = [-1, 0, 1;...
-1, 0, 1;...
-1, 0, 1];
moves_allowed = ones(M,N,9); % Define moves allowed for each position
moves_allowed(1,:,[1 4 7])=0;
moves_allowed(end,:,[3 6 9])=0;
moves_allowed(:,1,[1 2 3])=0;
moves_allowed(:,end,[7 8 9])=0;
moves_allowed(:,:,~move_grid)=0;
curr_pos = [1,1]; steps = 0; tries = 1;
while path(M,N) == 0
temp = reshape(moves_allowed(curr_pos(1),curr_pos(2),:),...
size(move_nums));
poss_move = move_nums(temp==1);
if ~overlap_allowed
temp_poss_move = ones(1,length(poss_move));
for i = 1 : length(poss_move)
if path(curr_pos(1)+move_inc(poss_move(i)),...
curr_pos(2)+move_inc(poss_move(i)+9)) == 1
temp_poss_move(i) = 0;
end
end
poss_move = poss_move(temp_poss_move == 1);
end
temp_poss_move = ones(1,length(poss_move));
for i = 1 : length(poss_move)
if sum(poss_move(i)==[1,2,3,4,5,6]) == 1
if sum(path(:,curr_pos(2)+move_inc(poss_move(i)+9))) ...
>= M_allowed
temp_poss_move(i) = 0;
end
end
if sum(poss_move(i)==[1,2,4,5,7,8]) == 1
if sum(path(curr_pos(1)+move_inc(poss_move(i)),:)) ...
>= N_allowed
temp_poss_move(i) = 0;
end
end
if sum(poss_move(i)==[1,3]) == 1
if sum(path(:,curr_pos(2))) >= M_allowed
temp_poss_move(i) = 0;
end
end
if sum(poss_move(i)==[1,7]) == 1
if sum(path(curr_pos(1),:)) >= N_allowed
temp_poss_move(i) = 0;
end
end
end
poss_move = poss_move(temp_poss_move == 1);
rand_move = randperm(length(poss_move));
curr_move = poss_move(rand_move(1));
curr_pos = curr_pos + [move_inc(curr_move),move_inc(curr_move+9)];
path(curr_pos(1),curr_pos(2)) = path(curr_pos(1),curr_pos(2))+1;
steps = steps + 1;
if sum(path(:,curr_pos(2))) > M_allowed || ...
sum(path(curr_pos(1),:)) > N_allowed
path = zeros(M,N); path(1) = 1; % Initialize path
curr_pos = [1,1]; steps = 0; tries = tries+1;
end
end
disp(tries) % Number of redo times (because of definition violations)
disp(steps) % Number of steps in output path
disp(path) % Matrix of path
3 件のコメント
Dr. Seis
2012 年 1 月 25 日
It worked in general, but it wasn't well suited for large matrices. I tried it on a 50x50 matrix and after waiting several minutes I had to kill it. With the modifications (included above) it took less than a second to reach a solution.
参考
カテゴリ
Help Center および File Exchange で GPU Computing in MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!