I agree with Tal Darom, pdist2
is exactly the function you need. It finds the distance for each pair of coordinates specified in two vectors and NOT the distance between two matrices.
So I'm pretty sure in your case you want this:
pdist2([Y(:), Z(:)], [Y(:), Z(:)])
The matrix [Y(:), Z(:)]
is a list of every possible coordinate combination over the 2D space defined by Y-Z. If you want a matrix containing the distance from each point to each other point then you must call pdist2
on this matrix with itself. The result is a 2D matrix with dimensions numel(Y)
x numel(Y)
and although you haven't defined it I'm pretty sure that both Y
and Z
are n*m
matrices meaning numel(Y) == n*m
EDIT:
A more correct solution suggested by @Shai is just to use pdist since we are comparing points within the same matrix:
pdist([Y(:), Z(:)])