code of trace path
Show older comments
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 Comments
Walter Roberson
on 22 Jan 2012
Are you trying to say that you want to create paths from (1,1) to (3,3) in which any one row or column is visited at most twice?
Is the path to be 4-connected or 8-connected (diagonal moves allowed) ?
Are you trying to generate all such paths, or the shortest such path, or any one such path?
huda nawaf
on 23 Jan 2012
huda nawaf
on 23 Jan 2012
Walter Roberson
on 23 Jan 2012
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?
huda nawaf
on 23 Jan 2012
Accepted Answer
More Answers (3)
Dr. Seis
on 23 Jan 2012
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 Comments
Dr. Seis
on 23 Jan 2012
I think I might understand - if you have an MxN matrix to trace the path through, then the sum of each column must be <= M-1 and the sum of each row must be <= N-1. Let me know if this is what you mean.
Walter Roberson
on 23 Jan 2012
Hmmm, exact number of times allowed is not as clear as I thought I had pinned down. We will need clarification on this. Exactly 2 simplifies to the logic I gave.
huda nawaf
on 24 Jan 2012
Dr. Seis
on 24 Jan 2012
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
on 24 Jan 2012
4 Comments
huda nawaf
on 24 Jan 2012
Dr. Seis
on 24 Jan 2012
Oh, ok. So the sum across all rows and all columns cannot exceed 2 for all MxN matrices?
Walter Roberson
on 24 Jan 2012
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.
huda nawaf
on 24 Jan 2012
Dr. Seis
on 24 Jan 2012
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 Comments
Dr. Seis
on 24 Jan 2012
If you are going to be looking for pathways through very large MxN matrices, then I have a "quick fix" for that too. Otherwise for 5x5 matrices, it shouldn't take but a few tries to get an acceptable path.
huda nawaf
on 25 Jan 2012
Dr. Seis
on 25 Jan 2012
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.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!