xy = [xCentroids, yCentroids];
distances = pdist2(xy, xy);
imshow(distances, []);
axis on;
title('Quantized Distances', 'FontSize', fontSize);
ylabel('From this particle', 'FontSize', fontSize);
xlabel('To this particle', 'FontSize', fontSize);
drawnow;
for row = 1 : size(distances, 1);
distances(row, :) = sort(distances(row, :), 'ascend');
end
imshow(distances, []);
axis on;
title('Sorted Quantized Distances', 'FontSize', fontSize);
ylabel('From this particle', 'FontSize', fontSize);
xlabel('To this particle', 'FontSize', fontSize);
drawnow;
distances = sortrows(distances, size(distances, 2));
imshow(distances, []);
axis on;
numberOfPoints = size(distances, 1);
caption = sprintf('Sorted Quantized Distances of %d Particles', numberOfPoints);
title(caption, 'FontSize', fontSize);
ylabel('From this particle', 'FontSize', fontSize);
xlabel('To this particle', 'FontSize', fontSize);
drawnow;
n = 1;
nthNeighborDistances = distances(:, n+1);
hold off;
histogram(nthNeighborDistances, 'Normalization', 'pdf');
axis on;
grid on;
suffix = 'th';
if n == 1
suffix = 'st';
elseif n == 2
suffix = 'nd';
elseif n == 3
suffix = 'rd';
end
caption = sprintf('Histogram of %d%s closest quantized distances', n, suffix);
title(caption, 'FontSize', fontSize);
ylabel('Count (# of particles)', 'FontSize', fontSize);
xlabel('Distance', 'FontSize', fontSize);
drawnow;
pd = fitdist(nthNeighborDistances, 'LogNormal')
xl = xlim;
x_values = linspace(xl(1), xl(2), 100);
y = pdf(pd, x_values);
hold on;
plot(x_values, y, 'LineWidth',2);
legend('Actual', 'Log-Normal Fit', 'Location', 'east');