Try
mat[cbind(seq(along=pos),pos)]
Rick Becker
> 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 leng=
> th
> 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.
>
> Thanks in advance.
> PI
>
> S-PLUS : Copyright (c) 1988, 2002 Insightful Corp.
> S : Copyright Lucent Technologies, Inc.
> Version 6.1.2 Release 2 for Linux 2.2.12 : 2002=20
> Working data will be in .Data
>
>
> Dr. Pierre Ilouga
> Screening Operations/Discovery Informatics
> EVOTEC AG
> Schnackenburgallee 114
> 22525 Hamburg
> Germany
>
> pierre.ilouga@evotec.com
> www.evotec.com
> =20 =20
>
>
> Evotec AG Hamburg. Amtsgericht Hamburg HRB 68223
> Vorstand: J=F6rn Aldag (Vorsitzender)
> Aufsichtsratsvorsitzender: Prof. Dr. Dr. h.c. mult. Heinz Riesenhuber
|