我想创建一个矩阵(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 中绘制由 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)