[R] From Distance Matrix to 2D coordinates
Peter Langfelder
peter.langfelder at gmail.com
Thu Dec 15 19:58:46 CET 2011
On Thu, Dec 15, 2011 at 10:47 AM, Lorenzo Isella
<lorenzo.isella at gmail.com> wrote:
> Thanks a lot!
> Precisely what I had in mind.
> One last question (an extension of the previous one): can this be extended
> to points in 3D? Once again, given the distance matrix, can I reconstruct a
> set of coordinates (among many possible) for the points in three-dimensional
> space?
> Cheers
>
> Lorenzo
Yes, you need to specify argument 'k=3' to cmdscale which instructs it
to 'scale' the input into 3 dimensions (the default is 2).
nPoints = 10;
nDim = 3;
set.seed(10);
points = matrix(runif(nPoints * nDim), nPoints, nDim);
# Their distance:
dst = dist(points)
# Classical multidimensional scaling
mds = cmdscale(dst, k=nDim);
# Distance of the points calculated by mds
dst2 = dist(mds);
# The two distances are equal
all.equal(as.vector(dst), as.vector(dst2))
HTH
Peter
More information about the R-help
mailing list