Skip to content
Prev 297173 / 398500 Next

How to index a matrix with different row-number for each column?

I think you are looking for

mt[cbind(vt, seq_along(vt))]

which uses some trickines known as matrix indexing (basically, the two
columns created by cbind() are considered row & col indices -- then
seq_along() gives the col1,col2, bit as needed.

HTH,

Michael
On Tue, Jun 12, 2012 at 10:28 AM, Zhenjiang Lan <lan.zhenjiang at gmail.com> wrote: