Is there an easier way to calculate the average Manhattan distance between a set of points easier than I have it in my code? I have a matrix, which contains a set of 2D points (the columns corespond to the x and y coordinates).
%for the first point
[idx,~]=dsearchn(isec(2:end,:),isec(1,:));sum=pdist2(isec(1,:), isec(idx+1,:), 'cityblock');%for the rest of the points
for i=2:size(isec,1)-1 others=[isec(1:i-1,:); isec(i+1:end,:)]; [idx,~]=dsearchn(others,isec(i,:)); man=pdist2(isec((i,:), others(idx,:), 'cityblock'); sum=sum+man;end%for the last point
[idx,~]=dsearchn(isec(1:end-1,:),isec(end,:));sum=sum+pdist2(isec(end,:), isec(idx,:), 'cityblock');avg_dist=sum/size(isec,1);
As dsearch cannot calculate other distances than euclidean, I use it to find the index of the nearest point. After that I have to call the pdist2 function to measure the cityblock distance. What complicates things even more is the fact, that I have to exclude the current point (to which I'm trying to find the closest one); that's the purpos of my 'others' variable and the 2 pieces of code above and below the for-cycle.
To give a bit of context:
The goal is image segmentation. The points are actually line intersections in a grid. I want to calculate the average Manhattan distance in order to use the result as a treshhold to determine which points are roughly in the given row/column (unlike in the case of crosswords, sudokus, nonograms I'm not interested in the square areas between the lines, rather in the intersections).
Best Answer