MATLAB Answers

Select and Save the points that have 3 neighbors exactly 1 unit of distance away.

1 view (last 30 days)
James Peach
James Peach on 22 Sep 2020
Commented: James Peach on 23 Sep 2020
I am trying to select and save points to an array. I want to get all the points that have 3 points exaclty 1 unit away. I have set up a matrix that contains all the distance values between the points, but I am struggling to get points that meet the criteria. My code below is below. Thanks in advance.
load('A.mat');
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
SizeofA = size(A,1);
dist1a = nan(numel(x));
proximity = 1;
for i = 1:SizeofA
for j = 1:(i-1)
dist1a(i,j) = sqrt((x(i)-x(j)).^2 + (y(i)-y(j)).^2 + (z(i)-z(j)).^2);
dist1a(j,i) = dist1a(i,j);
end
end
i2keep = min(dist1a) == proximity && histc(dist1a)> 1;
keep_x1 = x(i2keep);
keep_y1 = y(i2keep);
keep_z1 = z(i2keep);
B = [keep_x1, keep_y1, keep_z1];

  1 Comment

James Peach
James Peach on 23 Sep 2020
Id like to do something along the lines of this but by row instead of all at once.
https://www.mathworks.com/matlabcentral/answers/142281-count-the-number-of-times-a-value-occurs-in-a-specific-of-an-array

Sign in to comment.

Answers (1)

sushanth govinahallisathyanarayana
You could loop through the points and obtain a distance matrix dist
for i=1:size(A,1)
dist(:,i)=sum((A-A(i,:)).^2)
end
thr=1e-5; % example
dist_th=sum(abs(dist-1)<=thr);
% thresholding distances which are nearly exactly one, and finding the number of points that obey it for each point. The threshold of 1e-5 is so that points which are not exactly one unit away, but are 0.99999 units away will be included.
neighbour_pts=A(dist_th==3,:) % finds points that have 3 such neighbours
Hope this helps.

  1 Comment

James Peach
James Peach on 23 Sep 2020
I already have a distance matrix but I am trying to use logical statements to grab all the points that have 3 or more 1 values inside the rows.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!