The diag() function in R

后端 未结 2 407
我寻月下人不归
我寻月下人不归 2021-01-18 18:40

Is there a way to use the diag() function in a Matrix without using the built-in function or iteration?

   M<-matrix(1:9, ncol=3) # make a m         


        
2条回答
  •  一整个雨季
    2021-01-18 19:13

    Just subset based on another matrix:

    > diag(M)
    [1] 1 5 9
    > M[matrix(rep(sequence(ncol(M)), 2), ncol = 2)]
    [1] 1 5 9
    

    The above would run into a problem in a non-square matrix, so we modify it as below.

    As your function, one answer for question 5b could be:

    q5b <- function(M) { 
      A <- sequence(ncol(M))[sequence(min(nrow(M), ncol(M)))]
      M[cbind(A, A)]
    }
    

    Update: Benchmarks are always fun

    library(microbenchmark)
    
    fun1 <- function(M) diag(M)
    fun2 <- function(M) M[row(M) == col(M)]
    fun3 <- function(M) {
      A <- sequence(ncol(M))[sequence(min(nrow(M), ncol(M)))]
      M[cbind(A, A)]
    }    
    
    set.seed(1)
    M <- matrix(rnorm(1000*1000), ncol = 1000)
    
    microbenchmark(fun1(M), fun2(M), fun3(M), times = 100)
    # Unit: microseconds
    #     expr       min        lq     median        uq        max neval
    #  fun1(M)  4654.825  4747.408  4822.8865  4912.690   5877.866   100
    #  fun2(M) 53270.266 54813.606 55059.0695 55749.062 200384.531   100
    #  fun3(M)    66.284    82.321   118.8835   129.361    191.155   100
    

提交回复
热议问题