在MATLAB中绘制一个正方形网格 [英] Plotting a grid of squares in MATLAB

查看:2026
本文介绍了在MATLAB中绘制一个正方形网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在2D笛卡尔坐标系中绘制一个正方形,其角落在(±1,±1)处。我想进一步将它分成400个更小且相等的正方形,每个正方形的边长都是 0.1

I would like to plot a square in a 2D cartesian coordinate system with its corners at (±1,±1). I would like to further divide it to 400 smaller and equal squares each with an edge length of 0.1.

我怎么能在MATLAB中做到这一点?

How can I do this in MATLAB?

推荐答案

就像我必须解决的一个问题。我在下面做的是用meshgrid获取所有点的坐标。然后,我得到从everey点到pdist的每个其他点的距离,当距离为1时,它是我们要绘制的连接。然后我们绘制所有这些线。

This looks like a problem I had to solve. What I do below is get the coordinates of all the points with meshgrid. Then I get the distance from everey point to every other point with pdist, when the distance is 1 its is a connection we want to draw. Then we plot all those lines.

%# enter your prerequisites
I=400; R=0.1; N=sqrt(I); M=sqrt(I);

%# create vertices on square grid defined by two vectors
[X Y] = meshgrid(1:N,1:M); X = X(:); Y = Y(:); 

%# create adjacencymatrix with connections between all neighboring vertices
adjacency = squareform( pdist([X Y], 'cityblock') == 1 );

%# plot adjacenymatrix on grid with scale R and origin at the center
[xx yy] = gplot(adjacency, [X Y]); 
xx = xx-round(sqrt(I)/2); %# this centers the origin
yy = yy-round(sqrt(I)/2);
plot(xx*R, yy*R)

这篇关于在MATLAB中绘制一个正方形网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆