Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想计算上/下三角矩阵(对角线)的行/列和。例子:
m <- matrix(1:9, nrow=3) #result: upperRowSums(m) ## [1] 12 13 9 lowerRowSums(m) ## [1] 1 7 18
我知道这可以通过简单的for循环来完成,但我想避免这种情况。我正在寻找纯粹的R解决方案。
for
R
离开:
rowSums(m * upper.tri(m, diag=TRUE)) #[1] 12 13 9