How to subset a matrix with different column positions for each row? [duplicate]

自古美人都是妖i 提交于 2019-11-28 13:48:36

We can try the row/column indexing

M[cbind(1:nrow(M), v)]
#[1] 11  2  3  9  5

Just for fun, here's an another solution using a vector indexing

t(M)[v + (seq_len(nrow(M)) - 1) * ncol(M)]
# [1] 11  2  3  9  5
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!