Trying to create a map

9 views (last 30 days)
Jarne van Gemert
Jarne van Gemert on 24 Apr 2020
Commented: Jarne van Gemert on 29 Apr 2020
Hello,
I am trying to make a map with nodes and obstacles to implement the A star algorithm.
this is my code so far
clear all
clc
clf
%define how many nodes so xmax*ymax nodes
xmax = 40;
ymax = 40;
zmax = 40;
%start and goal coordinates
start = [1,1,0];
goal = [(xmax-1),(ymax-4),(zmax-20)];
%create map with nodes
map = zeros(xmax,ymax,zmax);
%define objects, xy to infinitif to create object
map(20:35,20,10:20) = inf;
map(22:33,28:35,10:20) = inf;
map(4:7,1:8,10:20) = inf;
map(15,12:35,10:20) = inf;
%Heuristic Weight%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
weight = sqrt(2); %Try 1, 1.5, 2, 2.5
%Increasing weight makes the algorithm greedier, and likely to take a
%longer path, but with less computations.
%weight = 0 gives Djikstra algorithm
%Heuristic Map of all nodes
for x = 1:size(map,1)
for y = 1:size(map,2)
for z = 1:size(map,3)
if(map(x,y,z)~=inf)
H(x,y,z) = weight*norm(goal-[x,y,z]);
G(x,y,z) = inf;
end
end
end
end
surf(map)
hold all
And this is the error I get:
Error using matlab.graphics.chart.primitive.Surface
Value must be a scalar, vector or array of numeric type
Error in surf (line 145)
hh = matlab.graphics.chart.primitive.Surface(allargs{:});
Error in try_my_own_Astar (line 44)
surf(map)
I already tried this:
X = map(:,1);
Y = map(:,2);
Z = map(:,3);
surf([X,Y,Z])
But then my obstacles are not visible anymore. Does anyone know how to solve this?

Answers (1)

Walter Roberson
Walter Roberson on 24 Apr 2020
You map has three independent variables and one dependent variable (which in the code is all zero or inf). It is never possible to surf() cuboids.
Furthermore, because your values are entirely 0 or inf, and inf cannot be drawn, if you were able to surf() you would see nothing (constant zeros) except a hole where the inf are. That is not something you would want to draw.
You could do volumeViewer(map) but it will not be very interesting.
Now, you also construct G and H at certain locations but do not define it for other locations, and you do not initialize G or H. The size of G and H that results is not going to be constant: it is going to depend upon the maximum dimension where you happen to assign a value. This is not good programming practice.
You construct H that might possibly have interesting content in it, but you do not try to display it. I would suggest to you that it is not of interest go display map, but that it is potentially of interest to display H.
To display a 3D object with values at each node, you can use volumeViewer() or you can use isosurface() or you can use Vol3D V2 from the File Exchange.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!