如何提高以下代码的速度?
for (i in 1:nrow(training)){
score[training[i,1],training[i,2],training[i,4]] = training[i,3]
}
Training
是一个有四列的矩阵。我只想建立一个数组,其值是training[i,3]
根据上面的公式。
谢谢!
如何提高以下代码的速度?
for (i in 1:nrow(training)){
score[training[i,1],training[i,2],training[i,4]] = training[i,3]
}
Training
是一个有四列的矩阵。我只想建立一个数组,其值是training[i,3]
根据上面的公式。
谢谢!
您可以使用矩阵进行索引。以下是[
's 文档的相关部分:
A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector.
因此,在您的情况下,for
可以将循环替换为:
score[training[, c(1, 2, 4)]] <- training[, 3]