Distances from a single point

158 views Asked by At

I'm looking to generate a Euclidean distance matrix from one point [1,1]. This is what I have, however it doesn't work as intended:

a=rand(10,10);
a=sort(a); %sort example matrix it should be visible that distances should get bigger and bigger
a=pdist([ones(size(a));a]);

Please imagine the 10x10 matrix as an image. I want to get the distances from point A (here [1,1]) to a series of other points, namely the whole matrix. So the expected format would be the original matrix 10x10 size but with all the distances to point A.

Is there an easy way to do this that works?

2

There are 2 answers

4
rayryeng On BEST ANSWER

Since your ultimate goal is related to image processing, I'll assume you have the image processing toolbox. You can also use bwdist and set the top left corner of the input to true and let it fill in the rest. Note that the input is a binary image.

A = false(10, 10);
A(1, 1) = true;
B = bwdist(A);

bwdist computes the distance transform where each location of the output is set so that if the corresponding input location is false, the distance from that location to the closest non-zero pixel is calculated. For locations that are true, the output is naturally 0. Since there is only one non-zero pixel in the input at the top left corner, the rest of the image should calculate the distance to this pixel. The default method of distance is the Euclidean distance which is what you're after.

Also note that the returned type of bwdist is single, or single precision floating-point. Depending on your application, converting to full double which is the default MATLAB numeric data type may be preferred. Just cast the output with the double function.

B = double(bwdist(A));

Example Run

>> A = false(10, 10);
>> A(1,1) = true;
>> B = double(bwdist(A))

B =

         0    1.0000    2.0000    3.0000    4.0000    5.0000    6.0000    7.0000    8.0000    9.0000
    1.0000    1.4142    2.2361    3.1623    4.1231    5.0990    6.0828    7.0711    8.0623    9.0554
    2.0000    2.2361    2.8284    3.6056    4.4721    5.3852    6.3246    7.2801    8.2462    9.2195
    3.0000    3.1623    3.6056    4.2426    5.0000    5.8310    6.7082    7.6158    8.5440    9.4868
    4.0000    4.1231    4.4721    5.0000    5.6569    6.4031    7.2111    8.0623    8.9443    9.8489
    5.0000    5.0990    5.3852    5.8310    6.4031    7.0711    7.8102    8.6023    9.4340   10.2956
    6.0000    6.0828    6.3246    6.7082    7.2111    7.8102    8.4853    9.2195   10.0000   10.8167
    7.0000    7.0711    7.2801    7.6158    8.0623    8.6023    9.2195    9.8995   10.6301   11.4018
    8.0000    8.0623    8.2462    8.5440    8.9443    9.4340   10.0000   10.6301   11.3137   12.0416
    9.0000    9.0554    9.2195    9.4868    9.8489   10.2956   10.8167   11.4018   12.0416   12.7279
0
beaker On

You could use pdist2 to do that, but then you'd have to get the grid coordinates, calculate the distances and then rearrange it back into a matrix, so I'll just calculate the distance directly:

point = [1 1];   % point to calculate distance from
imgRows = 10;    % grid size
imgCols = 10;

a = sqrt( ([1:imgRows].' -  point(1)).^2 + ([1:imgCols] - point(2)).^2 );

If you're not using the latest version of MATLAB (or Octave) with automatic broadcasting, you'll need to use the bsxfun version instead:

a = bsxfun(@(x,y) sqrt(x.^2 + y.^2), ([1:imgRows] - point(1)).', ([1:imgCols] - point(2)));

Result:

a =

    0.00000    1.00000    2.00000    3.00000    4.00000    5.00000    6.00000    7.00000    8.00000    9.00000
    1.00000    1.41421    2.23607    3.16228    4.12311    5.09902    6.08276    7.07107    8.06226    9.05539
    2.00000    2.23607    2.82843    3.60555    4.47214    5.38516    6.32456    7.28011    8.24621    9.21954
    3.00000    3.16228    3.60555    4.24264    5.00000    5.83095    6.70820    7.61577    8.54400    9.48683
    4.00000    4.12311    4.47214    5.00000    5.65685    6.40312    7.21110    8.06226    8.94427    9.84886
    5.00000    5.09902    5.38516    5.83095    6.40312    7.07107    7.81025    8.60233    9.43398   10.29563
    6.00000    6.08276    6.32456    6.70820    7.21110    7.81025    8.48528    9.21954   10.00000   10.81665
    7.00000    7.07107    7.28011    7.61577    8.06226    8.60233    9.21954    9.89949   10.63015   11.40175
    8.00000    8.06226    8.24621    8.54400    8.94427    9.43398   10.00000   10.63015   11.31371   12.04159
    9.00000    9.05539    9.21954    9.48683    9.84886   10.29563   10.81665   11.40175   12.04159   12.72792