How can I create a loop to duplicate a square, representing buildings, to create a grid, representing a city, in a matrix?

3 views (last 30 days)
I am looking to create a matrix which represents an urban city's map to test path planning algorithms where zero is a free path and 1 is an obstacle.
For example a square shape with a starting corner at (5,5) representing a building will have the following code:
MAP(5:10, 5) = 1;
MAP(10, 5:10) = 1;
MAP(10:5, 10) = 1;
MAP(10, 5:10) = 1;
How can I create a loop or similar to create multiple buildings in the matrix in a grid form with given parameters:
Starting building coordinate
Building seperation distance
Area of building
Number of buildings
Any help would be appreciated!
Many thanks

Accepted Answer

Matteo Pellegri
Matteo Pellegri on 27 Feb 2021
this is not a simple problem unless you reduce it to a simple one. For example: are your builings all squares? Are they distributed uniformly?
If the answer to the those questions is yes then it can be solved with for loops, or even with matlab built-in functions.
You can start with something simple like this
A=16; %area of the building
N=4; %number of buildings
l = sqrt(A) %length of a side
d=1; %distance between buildings
S = l*sqrt(N)+d*(sqrt(N)-1) %total urban area
roads = 1+l:l+1:S-l %location of the roads
S = ones(S); %fill with buildings
S(:,roads)=0; %remove the roads
S(roads,:)=0 %remove the roads
In this case, the code is not designed to have roads wider than 1, but you can work out a way to introduce that.
I hope this could help you start your journey.

More Answers (0)



Community Treasure Hunt

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

Start Hunting!

Translated by