Hi All, (I have a degree in math, but I am too embarassed to ask my colleagues, so here goes:) I would like to get a vector normal (orthogonal) to a plane formed by two other vectors. In matlab I do this: v1 = [.4, .6, .8]; v2 = [.9, .7, .2]; nn = cross(v1,v2) (gives ~[-.48, .65, -.24] if I do R> cross(v1, v2), I get .94. Huh? Thanks for all your help, again. W
Hi! In which library is this function? I cant find it using help.search() on my R installation. Eryk *********** REPLY SEPARATOR *********** On 5/28/2004 at 12:22 PM wwsprague at ucdavis.edu wrote:>Hi All, > >(I have a degree in math, but I am too embarassed to ask my colleagues, >so here goes:) > >I would like to get a vector normal (orthogonal) to a plane formed by >two other vectors. In matlab I do this: > >v1 = [.4, .6, .8]; v2 = [.9, .7, .2]; nn = cross(v1,v2) (gives ~[-.48, >.65, -.24] > >if I do R> cross(v1, v2), I get .94. Huh? > >Thanks for all your help, again. > >W > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
> > if I do R> cross(v1, v2), I get .94. Huh?Meant to say "R> crossprod(v1, v2)" Sorry
wwsprague at ucdavis.edu wrote:> Hi All, > > (I have a degree in math, but I am too embarassed to ask my colleagues, > so here goes:) > > I would like to get a vector normal (orthogonal) to a plane formed by > two other vectors. In matlab I do this: > > v1 = [.4, .6, .8]; v2 = [.9, .7, .2]; nn = cross(v1,v2) (gives ~[-.48, > .65, -.24]Huh? I don't have access to Matlab. Can you tell me how cross() is defined in Matlab (it's not obvious to me - at least not at 10:40 pm - how can anything get negative)?> > if I do R> cross(v1, v2), I get .94. Huh?Ha! ;-) crossprod(v1, v2): 0.4*0.9 + 0.6*0.7 + 0.8*0.2 = 0.94 Uwe Ligges> Thanks for all your help, again. > > W > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
W is looking for the vector cross product (a specifically three-dimensional object important in physics and engineering). The crossproduct() function provides a matrix product so that crossprod(x,y) is t(x)%*%y, something completely different. For the three-dimensional case you could define the cross and dot products "%x%"<-function(a,b) {c(a[2]*b[3]-a[3]*b[2], -a[1]*b[3]+a[3]*b[1], a[1]*b[2]-a[2]*b[1])} "%.%%<-function(a,b) sum(a*b) It would make sense, of course, to check that the arguments actually were vectors of length 3. -thomas
Thomas Lumley <tlumley <at> u.washington.edu> writes: : : W is looking for the vector cross product (a specifically : three-dimensional object important in physics and engineering). The : crossproduct() function provides a matrix product so that crossprod(x,y) : is t(x)%*%y, something completely different. : : For the three-dimensional case you could define the cross and dot products : : "%x%"<-function(a,b) {c(a[2]*b[3]-a[3]*b[2], -a[1]*b[3]+a[3]*b[1], : a[1]*b[2]-a[2]*b[1])} Alternately, you could get the cofactors from solve: cross3 <- function(a,b) { m <- cbind(a,b,1) solve(m)[3,]*det(m) }