Creating grid with incremental numbers around specific coordinate

4 vues (au cours des 30 derniers jours)
Luís Duarte
Luís Duarte le 23 Fév 2021
Commenté : Walter Roberson le 23 Fév 2021
Hi
I'm trying to solve a problem on cody that involves creating a function (safety_map(N, R, C)) for the creation of a grid of safety levels around a specific coordinate.
As an example, if the input is safety_map(5, 2, 3), the result should be the following:
[3 2 2 2 3;
3 2 1 2 3;
3 2 2 2 3;
3 3 3 3 3;
4 4 4 4 4]
I started with something like this:
gr = zeros(N)
for i = 1:(N-1)
r1 = R - i
r2 = R + i
c1 = C - i
c2 = C + i
For a simpler matrix or if 1 is at the center of the matrix I can write some code. But when eventually r1 or c1 <= 0 and r2 or c2 > N I strugle bad. The only way I see it is to write every condition possible...
Does anyone have some ideas on how to write a smaller function?
Thanks

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Fév 2021
N = 5; r = 2; c = 3;
temp = true(N,N);
gr = bwdistgeodesic(temp, c, r) + 1
gr = 5×5
3 2 2 2 3 3 2 1 2 3 3 2 2 2 3 3 3 3 3 3 4 4 4 4 4
  2 commentaires
Luís Duarte
Luís Duarte le 23 Fév 2021
Thanks!
But I don't have Image Processing tool box, and to submit my answer on cody I would need a solution that doesn't need addons
Walter Roberson
Walter Roberson le 23 Fév 2021
for i = 1:(N-1)
r1 = R - i
You are trying to define the coordinates for a box that you are going to write into. But one or more edges of the box might be outside the array. Consider that this has two effects:
  1. This might imply that you should not draw the edge
  2. The edges drawn in the perpendicular direction need to stop at the boundary
You only have a small number of cases to deal with.
  • if this edge would be outside the matrix for its row/column, then do not draw it at all
  • otherwise, you can use max() and min() to determine the drawing limits

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Two y-axis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by