【问题标题】:draw circle around data in a grid (MATLAB)在网格中的数据周围画圈(MATLAB)
【发布时间】:2015-03-19 05:50:06
【问题描述】:

我有 100 个数据点分散在一个区域内。现在我用 6x6 大小的网格划分区域,如下图所示:

figure http://s2.postimg.org/c6zk68myf/IGN2.png

现在我需要在网格的一个框内画一个圆圈,这样如果有超过 1 个点,我就可以在一个框中对点进行分组。如果是这种情况,如果一个盒子里没有点或一个点,盒子里就不会有圆圈。

知道我应该怎么做吗?

这是我在上面创建的绘图的 MATLAB 代码:

xm=100;
ym=100;

%x and y Coordinates of the Sink
sink.x=0.5*xm;
sink.y=0.5*ym;

%Number of Nodes in the field
n=100

figure(1);
for i=1:1:n
    S(i).xd=rand(1,1)*xm;
    XR(i)=S(i).xd;
    S(i).yd=rand(1,1)*ym;
    YR(i)=S(i).yd;
    S(i).G=0;
    %initially there are no cluster heads only nodes
    S(i).type='N';

        plot(S(i).xd,S(i).yd,'o');
        hold on;

   
end
NrGrid = 6;   
                               % Number Of Grids
x = linspace(0, 100, NrGrid+1);
[X,Y] = meshgrid(x);
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x',X,Y,'k');
 hold on
plot(Y,X,'k')
    set(gca, 'Box','off', 'XTick',[], 'YTick',[])
axis square
        
%First Iteration
figure(1);

预期结果:

http://i.share.pho.to/27ae061b_o.jpeg

【问题讨论】:

  • 很抱歉,我很难理解您想要什么。你到底想做什么?你在一个盒子里画一个圆圈是什么意思?你不是已经这样做了吗?
  • 对不起,我的任何误导性解释。实际上我想对彼此相邻的点进行分组。即在一个盒子里,如果有更多的点,即不止一个点,那么我想在一个盒子内覆盖一个圆圈中的所有点..
  • 很抱歉,我还是不明白。你如何定义点是否彼此接近?我们如何处理这些分组的点?也许您可以向我们展示您的预期输出的图像?
  • 这里是示例图片pho.to/9460P

标签: matlab


【解决方案1】:

所以你需要去那里的过程如下:

  1. 获取点在哪个网格中
  2. 计算最小半径圆(我用了this FEX submission,你可以自己做或者在自带的PDF里看看是怎么做的)
  3. 绘制圆圈

所以这里有一段代码完成 1-2

%% Where are the points?
% I am not going to modify your `S` variable, but you could do this inside
% it
points=[S(1:end-1).xd; S(1:end-1).yd];
gridind=zeros(size(points,2),1);
Grid=NrGrid^2;
for ii=1:NrGrid
    for jj=1:NrGrid
        % get points in the current square
        ind=points(1,:)>X(1,ii)& points(1,:)<X(1,ii+1)...
             & points(2,:)>Y(jj,1)& points(2,:)<Y(jj+1,1);
        % save index
        gridind(ind)=(ii-1)*NrGrid+(jj-1)+1;
    end
end

% now that we know in wich grid each point is lets find out wich ones are
% not
c=zeros(NrGrid^2,2);
r=zeros(NrGrid^2,1);

for ii=1:NrGrid^2
    if sum(gridind==ii)>1
        [c(ii,:), r(ii)] = minboundcircle(points(1,gridind==ii),points(2,gridind==ii));

    else
        c(ii,:)=nan;
        r(ii)=nan;
    end
end

这里有一段代码来绘制它们

theta=linspace(0,2*pi,20);

offs=1; % For beauty purposes
for ii=1:NrGrid^2
    if ~isnan(c(ii))
        xc=(r(ii)+offs).*cos(theta)+c(ii,1);
        yc=(r(ii)+offs).*sin(theta)+c(ii,2);
        plot(xc,yc,'r');
    end
end

axis([min(X(:)) max(X(:)) min(Y(:)) max(Y(:)) ])

结果:

您可以轻松地绘制椭圆来更改代码,例如使用this FEX submission

如果你有什么不明白的地方可以问,但应该直截了当。

【讨论】:

  • 如果我想在每个圆圈中用红色标记任何一点,我需要在哪里进行更改
  • @user38375 抱歉,我不明白你的意思。你能解释更多吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 2021-04-11
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
相关资源
最近更新 更多