Question: Hello! I am told I have an incorrect algorithm despite getting correct solution. I need to adjust code with limited guidance on what to change.

Hello! I am told I have an incorrect algorithm despite getting correct solution. I need to adjust code with limited guidance on what to change. The attached MATLAB code uses Periodic Boundary Conditions to create a map and table of nearest neighbors, that is listing the index of each point, the # of nearest neighbors (NNs) for that point, and then listing each of the NNs for that point. I am given a file that includes 300x4 matrix of 3003-D points where the first column of the file is the indices of each point, 2nd column are the x-values of those points, 3rd and 4th columns are y-values and z-values, respectively. We assume that the NNs of a point are within a radial distance of 2.8 units and the length of the cubic box is 17.86. From my understanding, the method works as follows: First we upload the points and impinge a box containing these points. To account for PBCs, we create a set of shifted boxes around every side of the original. Each box contains exact shifted replicas of the original points. There are 26 augmented/shift boxes, for a total 27 boxes with the original. I know I have done this part correctly. Then, we need to use these augmented points PBCs to find nearest neighbors. I need to address changes in this part of my algorithm, which are mentioned in code comments. These comments are very limited and only about WHERE I need to make changes, but little about what changes are. Can you please look at my MATLAB code, and fix mistakes and add the portion for nearest neighbor search using augmented points and shifted boxes. I am looking for code. Thank you very much in advance. I cannot use any built-in MATLAB function. I cannot attach files so I cannot provide dataset. I have the first 30 rows pasted here so it can be tested on those. I cannot attach MATLAB file, so it is included as image and pasted. I include my results in the attached image to show what result should be. I should get 4 NNs for each value, as I am now. I include the NN map output. I am not sure what is wrong with code that would allow me to still get correct results. I was told my replication is correct but NN search is not. This person didnt look close at code/result. Please help me fix code. Thank you.
Data: xyz300=[
0-3.684.063.03;
18.03-7.25-6.41;
2-1.944.438.39;
3-4.566.81-2.14;
4-6.582.01-7.60;
5-1.38-0.185.25;
6-2.05-2.63-3.42;
7-1.38-2.86-6.73;
87.31-2.033.21;
9-2.36-3.613.77;
10-4.108.37-5.61;
118.43-3.52-5.61;
127.757.416.12;
136.13-4.34-2.54;
148.135.64-7.63;
15-8.316.216.70;
16-4.875.786.27;
175.805.35-4.78;
18-2.40-2.255.65;
195.07-3.98-6.22;
202.165.865.34;
213.925.510.36;
225.952.82-6.72;
23-4.48-6.281.95;
248.667.352.71;
25-8.81-3.097.13;
260.762.897.14;
27-6.28-7.657.04;
28-1.265.41-5.14;
29-3.44-0.79-8.35]
MATLAB code with hardly helpful comments:
clear
clc
xyz300= load('xyz300.txt');
L =17.86;
r =2.8;
N = size(xyz300,1);
NNs = cell(N,1);
figure;
hold on;
for i =1:N
x_i = xyz300(i,2);
y_i = xyz300(i,3);
z_i = xyz300(i,4);
for dx =[-L,0, L]
for dy =[-L,0, L]
for dz =[-L,0, L]
% Generate shifted points in the current box
for j =1:N
x_j = xyz300(j,2)+ dx;
y_j = xyz300(j,3)+ dy;
z_j = xyz300(j,4)+ dz;
% add new loops here for NN search??
% for i =1:N
% for j =1:N+1(maybe)
% something here??? for NN search
%end
%end
%end
%% Now Calculate distance to point in the current box
%%check EVERY point to each other including original points
d = sqrt((x_i - x_j)^2+(y_i - y_j)^2+(z_i - z_j)^2);
% if distance is within range
if d = r && j ~= i
% Add index j to nearest neighbors of point i
NNs{i}=[NNs{i}, j];
plot3([x_i, x_j],[y_i, y_j],[z_i, z_j],'k-');
end
end
end %remove any leftover ends
end
end
end
scatter3(xyz300(:,2), xyz300(:,3), xyz300(:,4), 'filled','m');
hold off;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Nearest Neighbor Map');
grid on;
view(3);
% Create table for result
results = cell(N,3);
% Populate table
for i =1:N
results{i,1}= xyz300(i,1); % Index
results{i,2}= numel(NNs{i}); % Number of first nearest neighbors
if ~isempty(NNs{i})
results{i,3}= NNs{i}; % Nearest neighbor index
else
results{i,3}=[]; % No nearest neighbor found
end
end
T = cell2table(results, 'VariableNames', {'Index','# First Neighbors', 'First Neighbors'});
disp(T);
Hello! I am told I have an incorrect algorithm

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!