2017-07-14 10 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)を使用できます。これは、各セルのXのインデックスが各のradiusのインデックスを含むセル配列を返します。近くの点の数はYごとに異なる可能性があるため、1セルあたりのインデックスの数は異なる場合があります。だからあなたの場合

% 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

あなたが手で行うことができ、統計および機械学習ツールボックスを持っていない場合。 MATLABを持っている場合、特定の緑の点(x(i)y(i))の範囲R内にあるすべての「赤」のポイントを(あなたのコードから、彼らがAに含まれていると思われる)を見つけるために、あなたは

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

を使用することができます> = R2016は、そうでなければ

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

そして、w[x(i),y(i)]の範囲R内のすべてのアンカーポイントの論理1を含む論理アレイです。論理インデックスを使用して、A(w,:)を検索することができます。例えば、plot(A(w,1),A(w,2),'ks')はそれらを異なるマーカーでプロットします。

あなたが共同ですべてのあなたの緑のポイントのためにこれを行う必要がある場合、コードはMatlabの> = R2016に

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

になります。今度は、Wは、その行が赤い点であり、列が緑のマーカーであり、ペアが半径Rにある場合は論理1を含み、そうでない場合は0です。たとえば、any(W,2)を使用して、赤い点が緑のマーカーのいずれかの範囲内にあるかどうかを確認できます。

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; 
:あなたには、いくつかのrepmatの魔法で上記を変更する必要がR2016前のMatlabのために

関連する問題