Dear all,
Many thanks to all who took their valuable time to answer my request.
Terry Therneau
Stephen Kaluzny
Richard A. Becker
Bill Dunlap
Philippe Liège
Biggerstaff, Brad J.
Greg Snow
suggested
mat[cbind(seq(along=pos), pos)] or mat[cbind(1:length(pos), pos)]
Nevertheless there always exist guys who are the opinion that such simple
questions don't need to be sent to the Splus user group.
Nobody is perfect!
Regards,
PI
Dr. Pierre Ilouga
Screening Operations/Discovery Informatics
EVOTEC AG
Schnackenburgallee 114
22525 Hamburg
Germany
pierre.ilouga@evotec.com
www.evotec.com
-----Original Message-----
On Wed, Nov 07, 2007 at 06:11:36PM +0100, Ilouga, Pierre wrote:
> Dear Splus experts,
>
> Does anyone has an idea how to extract some elements from a matrix in an
> EFFICIENT way? The following example clarifies my question.
>
> Given a 5x3 matrix mat <- matrix(1:15, nrow=5, byrow=F) and a length
> 5 -vector of positions pos <- c(2, 1, 3, 3, 1), the goal is to extract
> the vector c(6, 2, 13, 14, 5) which corresponds to the column pos[j]
> (j in 1:3) and row i ( i in 1:5) without using for-loops and other
> memory consuming functions.
> I've solve it via lists. However the slowness of the command executions
> doesn't make it useful for monte carlo simulation.
Evotec AG Hamburg. Amtsgericht Hamburg HRB 68223
Vorstand: Jörn Aldag (Vorsitzender)
Aufsichtsratsvorsitzender: Prof. Dr. Dr. h.c. mult. Heinz Riesenhuber
|