2015-03-19 47 views
3

我在一个区域内散布了100个数据点。现在我分区内拥有6×6 equisized栅格和现在看起来根据如下图所示:在网格中绘制圆周数据(MATLAB)

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

+0

对不起,但我很难理解你想要什么。你想要做什么?你是什​​么意思,在一个盒子里画一个圆圈?你不是已经在做这个吗? – rayryeng 2015-03-19 05:55:52

+0

对不起,我有任何误导性的解释。实际上我想分组那些彼此相邻的点。我在一个盒子里,如果有更多的点数,即多于一点,那么我想在一个盒子内的一个圆圈内覆盖所有点。 – user38375 2015-03-19 06:13:35

+0

对不起,我还是不明白。你如何定义点是否彼此接近?我们对这些分组点做什么?也许你可以告诉我们你的预期输出是什么样子的图像? – rayryeng 2015-03-19 06:23:12

回答

4

所以,你需要去到那里的proccess如下:

  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(:)) ]) 

结果:

enter image description here

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

问如果你不明白的东西,但应该是直截了当的。

+1

很好地完成!这当然值得一个心理学家的徽章。 – rayryeng 2015-03-19 14:27:37

+0

我需要做出改变,如果我想在每个圆圈中标记红色的任何一个点 – user38375 2015-03-27 14:34:15

+0

@ user38375我道歉,我不明白你的意思。你能解释更多吗? – 2015-03-27 15:09:40