2017-07-14 41 views
0

我有一個像這樣的軌跡:我們假設每個紅色星形標記都可以將其座標廣播到距離其自己位置5個半徑範圍內的綠色圓圈標記。在matlab中選擇一個特定半徑內的n個點的列表?

enter image description here

我怎樣才能根據上面的說明選擇,每個綠色標記的n個紅點列表。提前致謝。

這是我的代碼,我提到了紅色點和綠色標記的座標。

%% Network Setup 
anchor_num=1; % Number of anchor node 
node_num=20; % Total nodes 
length1=70; % Area length 
anchor_x=0; % Intial position of anchor x coordinate 
anchor_y=0; % Intial position of anchor y coordinate 
anchormove=[];% Anchor trajectory 
width=40; % Area width 
r = 30;  
A = zeros(0,2); 
B = zeros(0,2); 
C = zeros(0,2); 
D = zeros(0,2); 
north = [ 0 6.9]; 
east = [ 6.9 0]; 
south = [ 0 -6.9]; 
west = [-6.9 0]; 
order = 4; 
for n = 1:order 
    AA = [B ; north ; A ; east ; A ; south ; C]; 
    BB = [A ; east ; B ; north ; B ; west ; D]; 
    CC = [D ; west ; C ; south ; C ; east ; A]; 
    DD = [C ; south ; D ; west ; D ; north ; B]; 
    A = AA; 
    B = BB; 
    C = CC; 
    D = DD; 
end 
% Plot network trajectory 
%Mtrix A contains the coordinate of red markers. 
A = [0 0; cumsum(A)] 
p=plot(A(:,1),A(:,2)) 
title('Plot of Hilbert trajectory'); 
set(p,'Color','magenta ','LineWidth',2); 
axis([0 100 0 100]); 
hold on 
% x and y are the coordinates of green markers 
x=rand(1,100)*100; 
y=rand(1,100)*100; 
scatter(x,y) 

anchormove(1,:)=A(:,1)' 
anchormove(2,:)=A(:,2)' 
idx=length(anchormove(1,:)); 
for i=1:idx-1 
    % Plot the moving anchor node 
    Ax=anchormove(1,i); 
    Ay=anchormove(2,i); 
    plot(Ax,Ay,'r*'); 
% Plot transmission range of the anchor node 
    axis([0 100 0 100]) 
    % hold on 
    pause(0.1) 
    %hold off 
end 

回答

0

您可以使用rangesearch(X,Y,radius)。它返回一個單元格數組,其中單元格包含對於每個點Y的給定radius內的點X的索引。由於每個Y附近點的數量可能會有所不同,因此每個單元的索引數量可能會有所不同。

所以你的情況:

% turn the two x and y vectors into [x y] column format. 
GreenPoints = [x;y].'; 
% get indexes of the points A for each Green point within 5 distance 
idx = rangesearch(A,GreenPoints,5); 

或更短:

idx = rangesearch(A,[x;y].',5); 
1

如果你沒有,你可以自己動手完成這樣的統計和機器學習工具箱。要查找所有的「紅色」點(從您的代碼似乎它們包含在A)的範圍內R從特定綠點(x(i)y(i)),你可以使用

w = sqrt(sum((A - [x(i),y(i)]).^2,2)) <= R; 

,如果你有Matlab的> = R2016,否則

w = sqrt(sum((A - repmat([x(i),y(i)],size(A,1),1)).^2,2)) <= R; 

然後,w是內的[x(i),y(i)]範圍R包含所有錨點邏輯1的邏輯陣列。您可以使用邏輯索引àlaA(w,:)來檢索它們。例如,plot(A(w,1),A(w,2),'ks')會用不同的標記來繪製它們。

如果你需要爲你的所有的綠色點做這個共同的代碼變得

W = sqrt(sum(abs((reshape(A,size(A,1),1,2) - reshape([x;y]',1,length(x),2)).^2),3)) <= R; 

Matlab的> = R2016。現在,W是一個矩陣,其行是紅色點,列是綠色標記,如果一對在半徑R內,則包含邏輯1,否則爲0。例如,您可以使用any(W,2)來檢查紅點是否在任何綠色標記的範圍內。

MATLAB的R2016之前,你需要修改上面的一些repmat魔術:

W = sqrt(sum(abs((repmat(reshape(A,size(A,1),1,2),1,length(x),1) - repmat(reshape([x;y]',1,length(x),2),size(A,1),1,1)).^2),3)) <= R; 
相關問題