我有一个n * n矩阵,想选择行号大于其列号的这些数据。
我尝试在R中使用循环,但问题是速度太慢。
matrix_new <- NULL
for (i in 1:nrow(matrix)) {
for (j in 1:ncol(matrix)) {
if (i > j) {
matrix_new <- c(matrix_new, matrix[i,j])
}
}
}
R中的双循环似乎很慢。是否有任何有效的方法可以做到这一点?
答案 0 :(得分:3)
下三角矩阵有一个基本的R函数:
m[lower.tri(m, diag = F)]
答案 1 :(得分:1)
$ awk 'NR==4{$0=(sub(/^#/,"") ? "" : "#") $0} 1' file
what are you doing now?
what are you gonna do? stab me?
this is interesting.
#This is a test.
go big
don't be rude.
$ awk 'NR==4{$0=(sub(/^#/,"") ? "" : "#") $0} 1' file |
awk 'NR==4{$0=(sub(/^#/,"") ? "" : "#") $0} 1'
what are you doing now?
what are you gonna do? stab me?
this is interesting.
This is a test.
go big
don't be rude.
答案 2 :(得分:1)
我们可以使用row/col
mat[row(mat) > col(mat)]