How to convert a sparse matrix into a matrix of index and value of non-zero element

前端 未结 3 745
青春惊慌失措
青春惊慌失措 2020-12-11 01:19

We can construct a sparse matrix from an index and value of non-zero element with the sparseMatrix or spMatrix. Is there any function convert a spa

3条回答
  •  盖世英雄少女心
    2020-12-11 01:47

    Use which with arr.ind:

    idx <- which(A != 0, arr.ind=TRUE)
    cbind(idx, A[idx])
    #      [,1] [,2] [,3]
    # [1,]    1    1    1
    # [2,]    3    3    2
    # [3,]    5    4    3
    

提交回复
热议问题