# Can someone help us with choosing the best syntax and nomenclature for this code?

1 view (last 30 days)
Steve on 22 Oct 2019
Commented: Steve on 22 Oct 2019
I have this code we are trying to run to create vectors from n closest points. However, it seems to be getting too complicated. Can someone help us to have a cleaner, more efficient code given the attched file (fpep.mat)? Below is the current code. Thanks in advance for your help.
close all;
clearvars;
n = 10;
for pt = 1 : 953
dist = sqrt((fpep(:,7)-fpep(pt,7)).^2 + (fpep(:,8)-fpep(pt,8)).^2);
[~, ascendIdx] = sort(dist); % Not really sure what this does or why there is a ~ used here. Can someone explain this?
xyNearest(pt,:,:) = fpep(ascendIdx(1:n),7:8);
bdirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),3) - xyNearest(pt,:,1)]);
bdirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),4) - xyNearest(pt,:,2)]);
cdirector_x(pt,:,:) = ([fpep(ascendIdx(1:n),5) - xyNearest(pt,:,1)]);
cdirector_y(pt,:,:) = ([fpep(ascendIdx(1:n),6) - xyNearest(pt,:,2)]);
ascendIdx(ascendIdx==pt) = []; %remove the source point. Not sure why the open brackets are used here. Can someone explain this?
end
chordx = ([xyNearest(:,:,1) - fpep(:,7)]);
chordy = ([xyNearest(:,:,2) - fpep(:,8)]);
chord = sqrt((chordx).^2 + (chordy).^2);
Steve on 22 Oct 2019
The "zoomed in" plot was manufactured in Paint. However, the other plot was obtained with the following code:
close all;
clearvars;
%xy = cell2mat(F_points)'; %[n x 2] matrix of (x,y) coordinates
xy = F_points';
pt = 499; % choose a point in xy and we'll find the 10 nearest neighbors.
% Euclidean distance between xy(p,:) and all other points
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
% Find the n closest values (excluding the point selected)
n = 10;
dist_ep = sqrt((fpep(:,1)-xy(pt,1)).^2 + (fpep(:,2)-xy(pt,2)).^2);
[Z,I] = sort(dist);
[~, ascendIdx_ep] = sort(dist_ep);
ascendIdx_ep(ascendIdx_ep==pt) = []; %remove the pt point
epNearest = fpep(ascendIdx_ep(1:n),:);
ptNearest = xy(I(1:n),:);
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = []; %remove the pt point
xyNearest = xy(ascendIdx(1:n),:);
IZ = ([I Z xy(I,1) xy(I,2)]); %% This shows the index number associated with each of the closest Fermat points
IZNearest = IZ(I(1:n),:);
% fpepNearest2 = fpep(I(1:n),:);
chordx = ([xyNearest(:,1) - xy(pt,1)]);
chordy = ([xyNearest(:,2) - xy(pt,2)]);
chord = sqrt((chordx).^2 + (chordy).^2);
director_x = ([fpep(:,1) - xy(pt,1)]);
director_y = ([fpep(:,2) - xy(pt,2)]);
director = sqrt((director_x).^2 + (director_y).^2);
figure()
for ii = 1 : length(chordx)
xvals = [xy(pt,2),xy(pt,2)+chordy(ii)];
yvals = [xy(pt,1),xy(pt,1)+chordx(ii)];
plot(xvals,yvals,'r-')
hold on
end
plot(xy(:,2),xy(:,1),'b.')
hold on
% Show the selected point
plot(xy(pt,2),xy(pt,1),'b.','MarkerFaceColor', 'y')
% Show nearest 'n' dots
plot(xyNearest(:,2),xyNearest(:,1),'r+')
plot(fpep(:,2),fpep(:,1),'k.')
plot(fpep(:,4),fpep(:,3),'k.')
plot(fpep(:,6),fpep(:,5),'k.')
axis equal