Sum of antidiagonal of a matrix

前端 未结 4 1898
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 14:29

I\'m trying to sum the elements along the antidiagonal (secondary diagonal, minor diagonal) of a matrix.

So, if I have a matrix m:

m <- matrix(c(2         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-05 15:10

    This is sometimes called the "secondary diagonal" or "minor diagonal".

    Another short solution:

    sum(diag(apply(m,2,rev)))
    

提交回复
热议问题