Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to populate matrix of indices with vector of values

Tags:

indexing

r

matrix

I have a matrix (m.idx) containing position elements of a vector I want to index.

> m.idx
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    3    4    5    6    7
[3,]    5    6    7    8    9

Suppose x is my vector.

x <- c(9,3,2,5,3,2,4,8,9)

I want to repopulate the matrix index with the corresponding position elements of x.

so I would have...

> m.pop
     [,1] [,2] [,3] [,4] [,5]
[1,]    9    3    2    5    3
[2,]    2    5    3    2    4
[3,]    3    2    4    8    9

I can kind of do it in a kludgy way with the following.

> m.pop <- t(matrix(t(matrix(x[c(t(m.idx))])),ncol(m.idx),nrow(m.idx)))

> m.pop
     [,1] [,2] [,3] [,4] [,5]
[1,]    9    3    2    5    3
[2,]    2    5    3    2    4
[3,]    3    2    4    8    9

But it seems like there may be an easier method to index the values. What is the best (and fastest/efficient for large sets) way to do this?

like image 338
pat Avatar asked Dec 09 '22 06:12

pat


2 Answers

How about:

m.idx[] <- x[m.idx]
m.idx
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    9    3    2    5    3
# [2,]    2    5    3    2    4
# [3,]    3    2    4    8    9

Or if you don't want to overwrite the m.idx matrix, you can do this instead:

m.pop <- m.idx
m.pop[] <- x[m.pop]

Added:

One other method, using structure, is also quite fast:

structure(x[m.idx], .Dim = dim(m.idx))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    9    3    2    5    3
# [2,]    2    5    3    2    4
# [3,]    3    2    4    8    9

When applied to the large m.idx matrix in Ananda Mahto's answer, the timings on my machine are

fun5 <- function() structure(x[m.idx], .Dim = dim(m.idx))
microbenchmark(fun1(), fun2(), fun3(), fun4(), fun5(), times = 10)
# Unit: milliseconds
#    expr       min        lq    median        uq       max neval
#  fun1()  303.3473  307.2064  309.2275  352.5076  353.6911    10
#  fun2()  548.0928  555.3363  587.6144  593.4492  596.5611    10
#  fun3()  480.6181  487.5807  507.5960  529.9696  533.0403    10
#  fun4() 1222.6718 1231.3384 1259.8395 1269.6629 1292.2309    10
#  fun5()  401.8450  403.7216  432.7162  455.4638  487.1755    10
identical(fun1(), fun5())
# [1] TRUE

You can see that structure is actually not too bad in terms of speed.

like image 175
Rich Scriven Avatar answered Dec 10 '22 20:12

Rich Scriven


matrix(x[m.idx],ncol=5)

     [,1] [,2] [,3] [,4] [,5]
[1,]    9    3    2    5    3
[2,]    2    5    3    2    4
[3,]    3    2    4    8    9
like image 43
DatamineR Avatar answered Dec 10 '22 20:12

DatamineR