我有一段代码搜索矩阵的哪些行boxes等于给定的向量x.这段代码使用了这个apply功能,我想知道它是否可以更优化?
x = floor(runif(4)*10)/10
boxes = as.matrix(do.call(expand.grid, lapply(1:4, function(x) {
seq(0, 1 - 1/10, length = 10)
})))
# can the following line be more optimised ? :
result <- which(sapply(1:nrow(boxes),function(i){all(boxes[i,] == x)}))
Run Code Online (Sandbox Code Playgroud)
我自己没有设法摆脱这个apply功能,但也许你会有比我更好的想法:)