function ALL = allnn3d(ARRAY);

% allnn3d(<array>) -- Lists the indices, the nearest neighbor, and the distance to that point.
%
%
% 
% At the moment, this is an N^2 algorithm.

DIST = [];  						% initialize the DISTance matrix
NN = [];							% initialize the Nearest Neighbor matrix
N = size(ARRAY,1);

for n = 1:N							% loop over every point and find distance
	for m = 1:N						% for each other point in the group
		if (m == n)
			DIST(n,m) = 999;
		else
			x1 = ARRAY(n,2);
			y1 = ARRAY(n,3);
			z1 = ARRAY(n,4);
			x2 = ARRAY(m,2);
			y2 = ARRAY(m,3);
			z2 = ARRAY(m,4);
			DIST(n,m) = dist3d(x1,y1,z1,x2,y2,z2);
		end		
	end
	NN = [NN; match(DIST(n,:),min(DIST(n,:)))];
end


Neighbors = NN;


%%%%%%%%%**********


DIST = [];  						% initialize the DISTance matrix
NN = [];							% initialize the Nearest Neighbor matrix
N = size(ARRAY,1);

for n = 1:N							% loop over every point and find distance
	for m = 1:N						%  for each other point in the group
		if (m == n)
			DIST(n,m) = 999;
		else
			x1 = ARRAY(n,2);        % Takes the point under consideration out
			y1 = ARRAY(n,3);        %  of the array (row n).
			z1 = ARRAY(n,4);

			x2 = ARRAY(m,2);        % Checks each other point, (row m0).
			y2 = ARRAY(m,3);    
			z2 = ARRAY(m,4);      

			DIST(n,m) = dist3d(x1,y1,z1,x2,y2,z2);    % Checks the distances.
		end		
	end
    
    % Now, finds smallest distance in the DIST matrix, which is the NND for 
    % that point (row n). 
	%NN = [NN; match(DIST(n,:),min(DIST(n,:))) min(DIST(n,:))];
	NN = [NN; n min(DIST(n,:))];
end

FINAL = [NN(:,1) Neighbors NN(:,2)];

ALL = FINAL;



