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

조회 수: 3 (최근 30일)
Lama Hamadeh
Lama Hamadeh 2021년 6월 21일
편집: Lama Hamadeh 2021년 7월 19일
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.
  댓글 수: 6
Scott MacKenzie
Scott MacKenzie 2021년 6월 21일
Ok, so we're kinda back where we started. If you just want a random cell from the 25x25 grid, can't you just pick the cell at x=randi(25) and y=randi(25)?
Lama Hamadeh
Lama Hamadeh 2021년 6월 21일
But would that give me a point rather than a cell?

댓글을 달려면 로그인하십시오.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 6월 21일
편집: Scott MacKenzie 2021년 6월 21일
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
  댓글 수: 3
Scott MacKenzie
Scott MacKenzie 2021년 6월 21일
Setting faceColor of 'flat' overrides the default color mapping for mesh graphs and allows you to customize the mapping. The code creates a color map with just two colors, red (1st entry) and gray (2nd entry). The line m.CData(:,:) = 2 makes all the 25x25 faces gray. The "2" refers to the 2nd entry in the color map. In the loop, indices are selected at random with the color of corresponding face changed to red (1) for half a second. The loop repeats 10x, just as a demo.
Scott MacKenzie
Scott MacKenzie 2021년 6월 22일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 6월 22일
편집: Scott MacKenzie 2021년 6월 22일
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

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by