# determine which data matches
matches <- t(pop) == target # 't' due to matching in column order
# colSums equal to COLS will indicate matches
which(colSums(matches) == COLS)
Neat! .. somewhat similar to the solution I came up with in the
meantime, only yours works :-)
Thanks Jim.
Esmail
Dimitris Rizopoulos wrote:
try this:
match.pat <- function (mat, target, nomatch = -1) {
f1 <- do.call("paste", c(as.data.frame(mat), sep = "\r"))
f2 <- paste(target, collapse = "\r")
ind <- f1 %in% f2
if (any(ind)) which(ind)[1] else nomatch
}
Thanks! More R for me to sink my teet
Henrique Dallazuanna wrote:
Try this:
which(apply(t(m) == target, 2, all))
Wow! .. talk about concise! Neat! Thanks.
This will return all matches correct? So if I only wanted
the first I'd simply subscript [1] into it.
Do you think the fact that it searches the whole matrix instead
of stoppi
Try this:
which(apply(t(m) == target, 2, all))
On Wed, Jun 11, 2008 at 8:58 AM, Esmail Bonakdarian <[EMAIL PROTECTED]>
wrote:
> Hi,
>
> I have matrix of bits and a target vector. Is there an
> efficient way to search the rows of the matrix for the target?
> I am interested in the first row index
ijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
- Original Message -
From: "Esmail Bonakdarian" <[EMAIL PROTECTED]>
To:
Sent: Wednesday, June 11, 2008
This should work for you:
> create_bin_string <- function(len)
+ {
+ sample(0:1, len, replace=T)
+ }
>
> ROWS = 10
> COLS = 5
> set.seed(2)
> pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)
>
>
>
> target=c(1, 1, 0, 1, 1)
>
> # my population
> print(pop)
[,1] [,2] [,3] [,4]
Hi,
I have matrix of bits and a target vector. Is there an
efficient way to search the rows of the matrix for the target?
I am interested in the first row index where target is found.
Example:
> source("lookup.R")
[,1] [,2] [,3] [,4] [,5]
[1,]10110
[2,]11
7 matches
Mail list logo