Suppose I have a matrix of data A 1 2 3 4 5 6 7 8 9 And I have a vector of indexes 2,1,3 For row 1 of matrix A I want the 2nd element 2 For row 2 of matrix A I want the 1st element 4 For row 3 of matrix A I want the 3rd element 9. Is there a quick way to do it ? Tjun Kiat [[alternative HTML version deleted]]
On Dec 15, 2012, at 9:38 PM, Tjun Kiat Teo wrote:> Suppose I have a matrix of data > > A > > 1 2 3 > 4 5 6 > 7 8 9 > > > And I have a vector of indexes 2,1,3 > > For row 1 of matrix A I want the 2nd element 2 > For row 2 of matrix A I want the 1st element 4 > For row 3 of matrix A I want the 3rd element 9.A[ cbind( 1:3, c(2,1,3) ) ] -- David Winsemius Alameda, CA, USA
On 16/12/12 18:38, Tjun Kiat Teo wrote:> Suppose I have a matrix of data > > A > > 1 2 3 > 4 5 6 > 7 8 9 > > > And I have a vector of indexes 2,1,3 > > For row 1 of matrix A I want the 2nd element 2 > For row 2 of matrix A I want the 1st element 4 > For row 3 of matrix A I want the 3rd element 9. > > Is there a quick way to do it ?A[cbind(1:3,c(2,1,3))] cheers, Rolf Turner