Selecting specific elements from a matrix all at once

后端 未结 3 563
旧巷少年郎
旧巷少年郎 2021-02-05 23:53

Is there a way I can select a bunch of prespecified elements all at once from a matrix? Specifically, suppose I have the following matrix:

      58        59             


        
3条回答
  •  北海茫月
    2021-02-06 00:38

    A similar solution to that posted above, but one that deals with the situation of having a vector for the rows and a vector for the columns (which was my question when I came upon this thread) is as follows:

    > rows <- c(1,2,3,4,5)
    > cols <- c(2,3,4,5,6)
    > call <- cbind(rows,cols)
    > 
    > mat[call]
    [1] 3.772139 3.755554 5.999409 3.810169 3.796884
    

提交回复
热议问题