MATLAB: Creation of a distance matrix for a vector of XY (easting and northing) pairs

distance matrixhelp

Hi! I'm new to matlab and was wondering how to calculate a distance matrix between XY pairs from a GIS output (also has Z and a unique name). I'm trying to compute the distance matrix to start network analysis…
Here is what I have so far… I know I'm probably doing this wrong. Any help would be greatly appreciated! Am i using pdist correctly for this? I'm trying to find an alternative to the stock dist() function in R… and want to produce a matrix in meters after.
%%- load file, plot XYZ
X = data(:,4); % relative, easting meters
Y = data(:,5); % relative northing, latitude, meters
Z = data (:,6); % elevation sampled from raster
D = pdist2(X,Y,'euclidean')

Best Answer

  • pdist2() is not the correct function to use here. You want to compute the pairwise distance between points of one set; you should use pdist()
    D = pdist([X Y]);
    D is a linear array as described here: You can convert it into a distance matrix using squareform()
    DM = squareform(D);