Get the vector of values from different columns of a matrix

后端 未结 1 1066
再見小時候
再見小時候 2020-12-10 07:22

I have a matrix 10x4, and I have a vector that has 10 elements. Each element is an column index of that matrix that should be retrieved. Here is the example:



        
1条回答
  •  清歌不尽
    2020-12-10 07:40

    This should do it for you:

    M.mat[cbind(seq_along(Idx),Idx)]
    

    0 讨论(0)
提交回复
热议问题