[R] angle and distance between sets of vectors
Petr Pikal
petr.pikal at precheza.cz
Fri Sep 19 18:19:47 CEST 2003
Dear all
I want to find a distance between two points in three dimensional space and an
angle between two vectors from [0,0,0] to specified points. I searched archives
and found some solution using dist but I am not sure how to use it if I have
several sets of such points.
Here is some example
#Four points
y <- c(1,0,0)
z <- c(1,1,0)
x <- 2*z
v <- 2*x
# two sets
mata<-rbind(y,x)
> mata
[,1] [,2] [,3]
y 1 0 0
x 2 2 0
matb<-rbind(z,v)
> matb
[,1] [,2] [,3]
z 1 1 0
v 4 4 0
# I want a distances and angles
> dist(rbind(y,z))
[1] 1
> dist(rbind(y/sum(y),z/sum(z)))
[1] 0.7071068
> dist(rbind(x,v))
[1] 2.828427
> dist(rbind(x/sum(x),v/sum(v)))
[1] 0
# they are actually here but they are a little bit hidden
> dist(rbind(mata,matb))
y x z
x 2.236068
z 1.000000 1.414214
v 5.000000 2.828427 4.242641
> dist(rbind(mata/rowSums(mata),matb/rowSums(matb)))
y x z
x 0.7071068
z 0.7071068 0
v 0.7071068 0 0
Before I start programming a for loop (something like that)
for (i in 1:length(mata)) result<-dist(rbind(mata[i,],matb[i,]))
I would like to ask some more experienced if there is a way how to get a vector
of distances and angles directly?
Something like
some.experienced.use.of.dist(mata,matb) resulting in matrix or dataframe or 2
vectors or whatever
distance angle
1 1, .7071068
2 2.828427, 0
will be greatly appreciated.
Thanks
Petr Pikal
Petr Pikal
petr.pikal at precheza.cz
More information about the R-help
mailing list