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

1 view (last 30 days)
Jesal Patel on 27 Feb 2021
Answered: Matteo Pellegri on 27 Feb 2021
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

Matteo Pellegri on 27 Feb 2021
Hi,
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.
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
S = ones(S); %fill with buildings
In this case, the code is not designed to have roads wider than 1, but you can work out a way to introduce that.