Skip to content
Back to formatted view

Raw Message

Message-ID: <8A44B0BE-F0BF-45A7-97A1-F0B0B5AEF693@comcast.net>
Date: 2012-12-16T06:03:22Z
From: David Winsemius
Subject: Picking out certain elements in an matrix
In-Reply-To: <CAH=5_TUaJwHdL7g96N9GpeTFL2RRroL2CBSbWu_4HjMhBa7-XA@mail.gmail.com>

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