I am trying to find the intersection of an entire list with a polygon.
1 次查看(过去 30 天)
显示 更早的评论
Nodes = 100;
for i=1:Nodes
waypointsBegin(i,1) = (-4+(8*rand(1)));
waypointsBegin(i,2) = (-4+(8*rand(1)));
end
value = zeros(length(waypointsBegin));
for i=1:length(waypointsBegin)
value(i,1) = linspace(state(1),waypointsBegin(i,1),1);
value(i,2) = linspace(state(2),waypointsBegin(i,2),1);
inpoly = inpolygon(value(:,1),value(:,2), obstacles(1).x,obstacles(1).y);
intersection_coordinates = find(inpoly);
intersection1(i,1) = value(intersection_coordinates);
intersection1(i,2) = value(intersection_coordinates);
end
waypointsRemovedObs1 = intersection1(:,1) ~= 0 & intersection1(:,2) ~=0;
intersection1New(waypointsRemovedObs1,:) = [];
I put my code above for what I am trying to do. So essentially, I am writing an RRT function for a robot using behavior based navigation along with LiDAR for determing localization. The problem I am having here in the main RRT function is that I am generating my list of random numbers (max 100) and I am trying to filter this list by finding the intersection, through the center of the robot and every randomly generated pair of coordinates, with obstacles already created (obstacles(1).x and obstacles(1).y). So I am finding the line that connects the robot to the point and seeing if it interstects that set obstacle.
When running this with all my main code, I am getting an error "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." This is occuing at the point:
intersection_coordinates = find(inpoly)
I know why its occuring as intersection_coordinates will always be a 2x1 array while "value" will continue to increase in size every iteration.
Any idea how I may fix this or rewrite this code to run that way?
1 个评论
Jiri Hajek
2022-12-8
编辑:Jiri Hajek
2022-12-8
Hi, it's not really clear what is your question. As a general rule however, you must make sure that size and type of the array on the left and right side of equation are the same. You say that you are aware or this, so you just have make your choice unique...
回答(1 个)
Karan Singh
2023-9-6
Hi Pawel,
From what I understand, the code provided aims to generate random waypoints for a robot and check for intersections with obstacles.
The “find” function here is used to find the indices of “inpoly” where the condition is true, indicating an intersection. The resulting indices are stored in the “intersection_coordinates” variable, and the code assigns the intersection coordinates to “intersection1” by directly indexing “value” with “intersection_coordinates”.
However, this assignment is incorrect because “intersection_coordinates” can have multiple indices, and you cannot assign multiple values to a single element of “intersection1”.
To fix the error you are encountering and correctly store the intersection coordinates, you need to modify the assignment of “intersection_coordinates” and the subsequent assignment of “intersection1”. Here is an updated version of the code:
Nodes = 100;
waypointsBegin = zeros(Nodes, 2);
for i = 1:Nodes
waypointsBegin(i, 1) = -4 + (8 * rand(1));
waypointsBegin(i, 2) = -4 + (8 * rand(1));
end
value = zeros(length(waypointsBegin), 2);
intersection1 = zeros(length(waypointsBegin), 2);
for i = 1:length(waypointsBegin)
value(i, 1) = linspace(state(1), waypointsBegin(i, 1), 1);
value(i, 2) = linspace(state(2), waypointsBegin(i, 2), 1);
inpoly = inpolygon(value(:, 1), value(:, 2), obstacles(1).x, obstacles(1).y);
intersection_coordinates = find(inpoly);
if ~isempty(intersection_coordinates)
intersection1(i, 1) = value(intersection_coordinates(1), 1);
intersection1(i, 2) = value(intersection_coordinates(1), 2);
end
end
waypointsRemovedObs1 = intersection1(:, 1) ~= 0 & intersection1(:, 2) ~= 0;
intersection1New = intersection1(waypointsRemovedObs1, :);
In this updated code, the following changes have been made:
- The “waypointsBegin” matrix has been preallocated to improve performance.
- Similarly, the “value” and “intersection1” matrices have been preallocated to match the size of “waypointsBegin”.
- “intersection_coordinates” is being checked if it is empty before assigning values to “intersection1”. This ensures that only valid intersection coordinates are stored.
- The assignment of “intersection1” has been corrected by indexing the “value” matrix with “intersection_coordinates(1)” to obtain the first intersection coordinate.
Attached below are some documentation links that you may find helpful:
- MATLAB Documentation (mathworks.com)
- Language Fundamentals - MATLAB & Simulink (mathworks.com)
- Functions - MATLAB & Simulink (mathworks.com)
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!