# How to choose the middle of a random cell in a 2D grid

1 view (last 30 days)
Lama Hamadeh on 21 Jun 2021
Edited: Lama Hamadeh on 19 Jul 2021
Hi all,
I have a 2D grid that has 25 points equally spaced in both axes dividing the grid into little boxes/cells/pixels. Is there a way to choose the middle of a random box in this grid. My code is the follwoing:
Any help would be appreciated.
Thanks.
Lama Hamadeh on 21 Jun 2021
But would that give me a point rather than a cell?

Scott MacKenzie on 21 Jun 2021
Edited: Scott MacKenzie on 21 Jun 2021
OK, thanks for the clarification. I think this achieves what you are after. The added code randomly chooses vertices (points) but highlights in red the face (box) that is bounded on the lower-left by the point. The demo repeats ten times with a half-second pause to show each randomly selected box. An example screen ship is also shown.
%x axis
smin = 0;
L = 4;
ns = 25;
s = linspace(smin,L,ns);
%y axis
pmin = -1;
pmax = 1;
np = 25;
p = linspace(pmin,pmax,np);
%construct coordinates meshgrid
[S,P] = meshgrid(s,p);
%Each point in the grid represents a position on the boundary and a
%direction of travel.
% mesh needs X,Y and Z so create z
Z = zeros(size(S));
%Visualise the grid
figure;
m = mesh(S,P,Z,'Marker','.','MarkerFaceColor','k','EdgeColor',"k", 'facecolor', 'w')
axis equal tight
view(2);
xlabel('\$S\$','Interpreter','latex');
ylabel('\$P\$','Interpreter','latex');
set(gca,'TickLabelInterpreter','latex');
set(gca,'FontSize',16);
set(gcf, 'color', 'w');
hold on
% set color map to gray+red and make all faces gray
colormap([1 0 0; .9 .9 .9]);
m.FaceColor = 'flat';
m.CData(:,:) = 2;
for i=1:10
% pick a vertex at random
x = randi([1 24]);
y = randi([1 24]);
% show the corresponding face in red
m.CData(x,y) = 1;
% pause briefly, then restore color to gray
pause(.5);
m.CData(x,y) = 2;
end Scott MacKenzie on 22 Jun 2021
@Lama Hamadeh Thanks. I just posted another solution that you can consider as well. It does basically the same thing but is simpler. Instead of setting up the grid using mesh, the grid is created just as a series of x-lines and y-lines. The highlighting of cells is done by defining a rectangle of the correct size and changing its position at randon within the grid.

Scott MacKenzie on 22 Jun 2021
Edited: Scott MacKenzie on 22 Jun 2021
This solution is simpler. It creates the grid using lines instead of a mesh.
% x axis
smin = 0;
L = 4;
ns = 25;
s = linspace(smin,L,ns);
% y axis
pmin = -1;
pmax = 1;
np = 25;
p = linspace(pmin,pmax,np);
% width and height of each cell in grid
w = (L-smin) / ns;
h = (pmax-pmin) / np;
% set up figure and axes
figure;
set(gcf, 'color', 'w');
ax = gca;
ax.Color = [.9 .9 .9];
ax.XTick = smin:L;
ax.YTick = pmin:0.5:pmax;
ax.XLabel.String = '{\itS}';
ax.XLabel.FontSize = 12;
ax.YLabel.String = '{\itP}';
ax.YLabel.FontSize = 12;
% create grid as series of x/y lines
xline(s);
yline(p);
% define a rectangle, but set x-y coordinates and color in loop
r = rectangle('position', [0 0 w h], 'facecolor', 'none', 'edgecolor', 'none');
% pick cells at random and highlight in red for half second (10x)
for i=1:10
x = s(randi([1 length(s)-1]));
y = p(randi([1 length(p)-1]));
r.Position(1:2) = [x y];
r.FaceColor = 'r';
pause(0.5);
r.FaceColor = 'none';
end 