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?
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 totrue
and let it fill in the rest. Note that the input is a binary image.bwdist
computes the distance transform where each location of the output is set so that if the corresponding input location isfalse
, the distance from that location to the closest non-zero pixel is calculated. For locations that aretrue
, 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
issingle
, or single precision floating-point. Depending on your application, converting to fulldouble
which is the default MATLAB numeric data type may be preferred. Just cast the output with thedouble
function.Example Run