[R] which points within an ellipsoid? Sorting data in 3d

From: Kim Milferstedt <milferst_at_uiuc.edu>
Date: Tue 03 Apr 2007 - 15:15:38 GMT


in a three dimensional coordinate system, I'd like to find all my experimental data points that fall within an ellipsoid around a fixed coordinate. The fixed point is defined by (x.coord.point, y.coord.point, z.coord.point). The coordinates of the ellipsoid are given by the three vectors x,y,z.

In a previous version of my code, I simply used a box instead of an ellipsoid to sort my data, which was really easy as I only had to compare each data point to three coordinates. I used something like this...

XYZ.within <- which( x.data < x.coord.point + multipl &

                              x.data  > x.coord.point - multipl &
                              y.data  < y.coord.point + multipl &
                              y.data  > y.coord.point - multipl &
                        z.data  < z.coord.point + multipl &
                              z.data  > z.coord.point - multipl

But now I have many more coordinate sets to compare my data to. How can I find out in an efficient way which of the data points lie within the ellipsoid?



#mock-data for the display with rgl

data.1 <- xyz.coords(5,-2,17)
data.2 <- xyz.coords(15, -10,18)
data.3 <- xyz.coords(-19, 13,9)

#code I use to construct the ellipsoid

x.coord.point <- 4
y.coord.point <- -7
z.coord.point <- -3

radius <- 8

x.radius.multiplier <- 1
y.radius.multiplier <- 2
z.radius.multiplier <- 3

endpoint <- 350
interval <- 2
alpha <- rep(seq(0,endpoint, by
=interval),rep((endpoint+interval)/interval,(endpoint+interval)/interval)) beta <- c(rep(seq(0,endpoint, by =interval),(endpoint+interval)/interval))

x <-
x.coord.point+(x.radius.multiplier*radius)*cos(alpha*pi/180)*sin(beta*pi/180) y <- y.coord.point+(y.radius.multiplier*radius)*sin(alpha*pi/180) z <-

# using rlg to visualize the mock-data and the ellipsoid
plot.lim <- c(-20,20)
plot3d(x, y, z,

         xlim = plot.lim,
         ylim = plot.lim,
         zlim = plot.lim
points3d(data.1, col ="green", size = 6)
points3d(data.2, col ="red", size = 6)
points3d(data.3, col ="blue", size = 6)

Kim Milferstedt
University of Illinois at Urbana-Champaign Department of Civil and Environmental Engineering 4125 Newmark Civil Engineering Laboratory 205 North Mathews Avenue MC-250
Urbana, IL 61801
phone: (001) 217 333-9663
fax: (001) 217 333-6968
email: milferst@uiuc.edu

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Apr 04 01:22:51 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 03 Apr 2007 - 16:30:52 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.