-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestPath.m
More file actions
44 lines (41 loc) · 1.47 KB
/
Copy pathBestPath.m
File metadata and controls
44 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function [pathRow,pathCol,pathElev] = BestPath(elevation)
%Works out the best possible path for a N x M 2d array
% Will work out the best possible path and then return it as a path
% heading from west to east or the right side of the array to the left
% side of the array.
%Inputs: Elevations is a 2d array containing the elevation data
%Outputs: pathrow: A 1d array of row indicies for the bestpath
% pathcol: A 1d array of column indicies for the bestpath
% pathelev: A 1d array containing the elevations for the bestpath
%Author: Reshad Contractor
cost=inf(size(elevation));
cost(:,1)=0;
cols=zeros(size(elevation));
rows=zeros(size(elevation));
colcount=size(elevation,2);
rowcount=size(elevation,1);
for i=1:colcount-1
for j=1:rowcount
if j==1
k=0:1;
elseif j==rowcount
k=-1:0;
else
k=-1:1;
end
for k=k(1):k(length(k))
nextrow= j+k;
nextcol= i+1;
weightD=abs(elevation(nextrow,nextcol)-elevation(j,i));
prevweight=cost(j,i);
if (weightD+prevweight)<(cost(nextrow,nextcol))
cost(nextrow,nextcol)=(weightD+prevweight);
rows(nextrow,nextcol)=j;
cols(nextrow,nextcol)=i;
end
end
end
end
[pathCol,pathRow] = pathfinder(rows,cols,cost,elevation);
[pathElev,~] = FindPathElevationsAndCost(pathRow,pathCol,elevation);
end