Thanks for the solutions. Here are some time tests for ma and idx
being 100 X 100,000. The machine is a 2.16 GHz Intel MacBook Pro with
2 GB memory.
ma <- matrix(rnorm(1e7), nr = 100) # 100 X 100,000
idx <- matrix(round( runif(1e7, 1, 100) ), nr = 100)
# Original:
system.time( {
mb <- m
Turn each matrix into a data.frame and then use mapply with the "[" function,
converting back to matrix when done:
as.matrix(mapply("[", as.data.frame(ma), as.data.frame(idx)))
V1 V2
[1,] 10 14
[2,] 12 15
[3,] 11 13
On 1/19/07, Osorio Roberto <[EMAIL PROTECTED]> wrote:
> Consider a matrix l
Matrix subscripting can be used for this:
> mb <- ma[cbind(as.vector(idx), as.vector(col(idx)))]
> dim(mb) <- dim(ma)
> mb
[,1] [,2]
[1,] 10 14
[2,] 12 15
[3,] 11 13
Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide fo
]
[mailto:[EMAIL PROTECTED] On Behalf Of Osorio Roberto
Sent: Friday, 19 January 2007 4:15 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Vectorize rearrangement within each column
Consider a matrix like
> ma = matrix(10:15, nr = 3)
> ma
[,1] [,2]
[1,] 10 13
[2,] 11 14
[3,]
Consider a matrix like
> ma = matrix(10:15, nr = 3)
> ma
[,1] [,2]
[1,] 10 13
[2,] 11 14
[3,] 12 15
I want to rearrange each column according to row indexes (1 to 3)
given in another matrix, as in
> idx = matrix(c(1,3,2, 2,3,1), nr = 3)
> idx
[,1] [,2]
[1,]1