[R] comparing two matrices

Christos Hatzis christos at nuverabio.com
Sat Jan 20 23:56:09 CET 2007


Here is a slightly more compact version of your function which might run
faster (I did not test timings) since it does not use the sum:

apply(mat2, 1, function(x) which(apply(mat1, 1, function(y) all(x == y)) ==
TRUE))

-Christos
 
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Adrian Dusa
Sent: Saturday, January 20, 2007 5:15 PM
To: r-help at stat.math.ethz.ch
Subject: [R] comparing two matrices


Dear helpeRs,

I have two matrices:
mat1 <- expand.grid(0:2, 0:2, 0:2)
mat2 <- aa[c(19, 16, 13, 24, 8), ]

where mat2 is always a subset of mat1

I need to find the corersponding row numbers in mat1 for each row in mat2.
For this I have the following code:

apply(mat2, 1, function(x) {
    which(apply(mat1, 1, function(y) {
        sum(x == y)
        }) == ncol(mat1))
    })

The code is vectorized, but I wonder if there is a simpler (hence faster)
matrix computation that I miss.

Thank you,
Adrian


--
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list