如何获得椭圆的半轴长度?在R

时间:2013-08-16 16:49:18

标签: r geometry ellipse

我有这组x和y坐标:

x<-c(1.798805,2.402390,2.000000,3.000000,1.000000)
y<-c(0.3130147,0.4739707,0.2000000,0.8000000,0.1000000)
as.matrix(cbind(x,y))->d

我想计算包含这组点的椭球,我在包“cluster”中使用函数ellipsoidhull(),我得到:

> ellipsoidhull(d)
'ellipsoid' in 2 dimensions:`
 center = ( 2.00108 0.36696 ); squared ave.radius d^2 =  2`
and shape matrix =
x 0.66590 0.233106
y 0.23311 0.095482
  hence, area  =  0.60406

然而,对我来说,如何从这些结果中得到这个椭圆的半长轴的长度并不明显。

有什么想法吗?

非常感谢你。

蒂娜。

2 个答案:

答案 0 :(得分:5)

你可以这样做:

exy <- predict(ellipsoidhull(d)) ## the ellipsoid boundary
me <- colMeans((exy))            ## center of the ellipse

然后计算分别获得次轴和长轴的最小和最大距离:

dist2center <- sqrt(rowSums((t(t(exy)-me))^2))
max(dist2center)     ## major axis
[1] 1.264351
> min(dist2center)   ## minor axis
[1] 0.1537401

编辑使用轴绘制椭圆:

plot(exy,type='l',asp=1)
points(d,col='blue')
points(me,col='red')
lines(rbind(me,exy[dist2center == min(dist2center),]))
lines(exy[dist2center == max(dist2center),])

enter image description here

答案 1 :(得分:5)

半轴的平方是 形状矩阵的特征值乘以平均半径。

x <- c(1.798805,2.402390,2.000000,3.000000,1.000000)
y <- c(0.3130147,0.4739707,0.2000000,0.8000000,0.1000000)
d <- cbind( x, y )
library(cluster)
r <- ellipsoidhull(d)
plot( x, y, asp=1, xlim=c(0,4) )
lines( predict(r) )
e <- sqrt(eigen(r$cov)$values)
a <- sqrt(r$d2) * e[1]  # semi-major axis
b <- sqrt(r$d2) * e[2]  # semi-minor axis
theta <- seq(0, 2*pi, length=200)
lines( r$loc[1] + a * cos(theta), r$loc[2] + a * sin(theta) )
lines( r$loc[1] + b * cos(theta), r$loc[2] + b * sin(theta) )