【发布时间】:2020-05-21 16:32:58
【问题描述】:
我想找出矩阵中哪个向量(或行)与其他向量(行)的相关值最高。除了在这样的循环中执行此操作之外,还有其他解决方案吗:
mat <- matrix(rnorm(100), 5, 5)
for (i in 1:nrow(mat)){
for (j in 1:nrow(mat)){
# the correlation coefficients of each row
cor_val[[i]][[j]] <- cor(mat[i,], mat[j,])
# the average of the correlation coefficients of each row
cor_mean[[i]] <- mean(unlist(cor_val[[i]]))
}
}
# the index of the row with the highest correlation
Indx <- which.max(cor_mean)
【问题讨论】:
-
cor_mean[[i]] <- ...行应该放在嵌套循环的第一层。
标签: r correlation