小编Nil*_*ile的帖子

如何通过平均R中另一个矩阵的元素来创建矩阵?

我想创建一个矩阵(A),其中元素是另一个矩阵(B)的每四行的平均值.例如,矩阵A中第1行的元素应该是矩阵B中第1行到第4行的平均值.目前我已经使用了一个循环函数来获得它,但是矩阵的大小非常大,这使得循环很有时间耗时.我想知道是否有更好的方法可以做到这一点.这是一个例子

B = matrix(runif(10000, 0, 10), 100, 100)
A = matrix(0, floor(dim(B)[1]/4), dim(B)[2])
for (im in 1: floor(dim(B)[1]/4)){
    A[im, ] = colMeans(as.matrix(B[c((((im - 1)*4) + 1):(im*4)), ]))
}
Run Code Online (Sandbox Code Playgroud)

r matrix

2
推荐指数
1
解决办法
94
查看次数

在 R 中绘制由 quickhull 算法给出的凸包(convhulln 函数)

我需要在 R 中绘制由 quickhull 算法给出的凸包。这是一个例子。

library(geometry)
x1 <- rnorm(100, 0.8, 0.3)
y1 <- rnorm(100, 0.8, 0.3)
ConVexHull<-convhulln(cbind(x1,y1),"FA")
Run Code Online (Sandbox Code Playgroud)

ConVexHull$hull 给出一个 m 维索引矩阵,其中每一行定义一个暗维“三角形”。

我知道如何使用 chull 函数进行绘图,但我不确定 chull 是否提供与 convhulln 相同的外壳

  Plot_ConvexHull<-function(xcoord, ycoord, lcolor){
  hpts <- chull(x = xcoord, y = ycoord)
  hpts <- c(hpts, hpts[1])
  lines(xcoord[hpts], ycoord[hpts], col = lcolor)
} 
xrange <- range(c(x1))
yrange <- range(c(y1))
par(tck = 0.02, mgp = c(1.7, 0.3, 0))
plot(x1, y1, type = "p", pch = 1, col = "black", xlim = c(xrange), ylim =    c(yrange)) …
Run Code Online (Sandbox Code Playgroud)

plot geometry r convex-hull qhull

2
推荐指数
1
解决办法
1039
查看次数

标签 统计

r ×2

convex-hull ×1

geometry ×1

matrix ×1

plot ×1

qhull ×1