How to script this: returns all the x coordinates that distance between X and Y are bigger than r distant ?

2 views (last 30 days)
I put together this, which is returning the y coordinates that are having greater distance than 2 e.g.
%
rng(1); % For reproducibility
X = randn(50,2);%GreenPeak
Y = randn(4,2);%RedPeak;
%imshow(Ifinalb); hold on ;
h = zeros(3,1);
figure;
h(1) = plot(X(:,1),X(:,2),'bx');
hold on;
h(2) = plot(Y(:,1),Y(:,2),'rs','MarkerSize',10);
%title('Heterogenous Data')
%%-----------------------
%Choose weights for each dimension, and specify the chi-square distance function. The distance function must:
%Take as input arguments one row of X, e.g., x, and the matrix Z.
%Compare x to each row of Z.
%Return a vector D of length $n_z$, where $n_z$ is the number of rows of Z.
%Each element of D is the distance between the observation corresponding to x and the observations corresponding to each row of Z
hold on
w = [2; 2];
chiSqrDist = @(x,Z)sqrt((bsxfun(@minus,x,Z).^2)*w);
%Find the indices of the three nearest observations in X to each
%observation in Y.k=3
k = 1;
[Idx,D] = knnsearch(X,Y,'Distance',chiSqrDist,'k',k);
%[Idx,D]= knnsearch(X,Y,'dist','cityblock','k',k);
TableDisInd(:,1)=Idx(:,1);
TableDisInd(:,2)=D(:,1);
Indx2=find(TableDisInd(:,1)>=2);
TableDisInd2=TableDisInd(Indx2,:);
  4 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 2 Jun 2017
X1 = X ; % this your X coordinates, in which you want to remove one point
X1(Idx(1),:) = [] ; % remove the first index from Idx
  2 Comments
Peyman Obeidy
Peyman Obeidy on 3 Jun 2017
with a bit of modification here is the code;
w = [2; 2];
chiSqrDist = @(x,Z)sqrt((bsxfun(@minus,x,Z).^2)*w);
%Find the indices of the three nearest observations in X to each
%observation in Y.k=3
k = 1;
[Idx,D] = knnsearch(X,Y,'Distance',chiSqrDist,'k',k);
%[Idx,D]= knnsearch(X,Y,'dist','cityblock','k',k);
TableDisInd(:,1)=Idx(:,1);
TableDisInd(:,2)=D(:,1);
% you are introduceing what needs to be delete, so if you want everything
% bigger than 4 you need to delet everything smaller than 4
Indx2=find(TableDisInd(:,2)<4);
TableDisInd2=TableDisInd(Indx2,:);
%use the first col of "TableDisInd2" to find the index in X table
Idx2=TableDisInd2(:,1);
X1 = X ; % this your X coordinates, in which you want to remove one point
%X1(Indx2(:,1),:) = [] ; % remove the first index from Idx
X1(Idx2(:,1),:) = [] ; % remove all the close distances from X list

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Network Parameters in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!