Skip to content
Prev 343342 / 398506 Next

Euclidean Distance in 3 Dimensions

[I think 'this' is refering to the 'dist' function.]

Can you show how it is not working for you?  I.e., what does it
produce compared to what you want for a given input?

dist() does work on a 3-column (or n-column) matrix or data.frame,
which is how R generally represents 3 dimensional (or n dimensional)
data.  E.g.,

  > d <- data.frame(One=1:3, Two=c(3,5,8), Three=c(4,8,16))
  > d
    One Two Three
  1   1   3     4
  2   2   5     8
  3   3   8    16
  > dist(d)
            1         2
  2  4.582576
  3 13.152946  8.602325
  > as.matrix(dist(d)) # the matrix format makes further compuations easier
            1        2         3
  1  0.000000 4.582576 13.152946
  2  4.582576 0.000000  8.602325
  3 13.152946 8.602325  0.000000
  > which(as.matrix(dist(d))>8, arr.ind=TRUE)
    row col
  3   3   1
  3   3   2
  1   1   3
  2   2   3
  > sqrt(sum((d[,2] - d[,3])^2)) # the 2,3 or 3,2 element, by hand
  [1] 8.602325

I think it would help if you restated your problem.  I found the original
description confusing.  A small example, with the expected output, would
be very helpful.

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Thu, Aug 21, 2014 at 11:34 AM, Patzelt, Edward <patzelt at g.harvard.edu> wrote: