Re: [R] searching for specific row in matrix

2008-06-11 Thread jim holtman
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] [,5]
 [1,]01101
 [2,]10101
 [3,]10100
 [4,]11000
 [5,]10100
 [6,]00010
 [7,]00111
 [8,]11010
 [9,]10001
[10,]11011

 # 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)
[1] 10


On Wed, Jun 11, 2008 at 7: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 where target is found.

 Example:

 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
  [1,]10110
  [2,]11010
  [3,]00100
  [4,]10011
  [5,]10111
  [6,]11001
  [7,]10011
  [8,]00111
  [9,]01101
 [10,]00010

 target:  1 1 0 1 1

 Should return -1 (or some other indicator) since the
 target was not found in any of the rows.



 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
  [1,]00110
  [2,]10000
  [3,]10000
  [4,]11000
  [5,]11100
  [6,]00110
  [7,]01110
  [8,]00110
  [9,]11011
 [10,]10100

 target:  1 1 0 1 1

 Should return 9 since the target was  found in row 9


 If the target is found, it is no longer necessary to keep
 searching the rest of the matrix (which may be quite large)

 The data/size etc may change of course, but target will
 always have the same number of columns as the matrix.

 I tried variations of which, and a for loop
 comparing pop[i,] to target without much success, nor
 did google yield any results. I am hoping someone here
 can provide a suggestion.

 Thanks,

 EB


 -

 # Here is the code that generates the above data

 create_bin_string - function(len)
 {
  sample(0:1, len, replace=T)
 }

 ROWS = 10
 COLS =  5
 pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)



 target=c(1, 1, 0, 1, 1)

 # my population
 print(pop)

 # I am looking for the index of this in pop
 # if present (else -1?)
 cat(\ntarget: , target, \n)



 ##
 ## this is NOT working
 ## plus it would continue to search
 ## after it found the target
 ##
 for(i in ROWS)
   if (pop[i,] == target)
  cat(\nfound in row: , i, \n\n)

 __
 R-help@r-project.org 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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

__
R-help@r-project.org 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.


Re: [R] searching for specific row in matrix

2008-06-11 Thread Dimitris Rizopoulos

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
}
##
set.seed(1234)
mat - matrix(sample(0:1, 50, TRUE), 10, 5)

targ1 - mat[2, ]
match.pat(mat, targ1)

targ2 - rep(0, 5)
match.pat(mat, targ2)


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 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: r-help@r-project.org
Sent: Wednesday, June 11, 2008 1:58 PM
Subject: [R] searching for specific row in matrix



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,]11010
 [3,]00100
 [4,]10011
 [5,]10111
 [6,]11001
 [7,]10011
 [8,]00111
 [9,]01101
[10,]00010

target:  1 1 0 1 1

Should return -1 (or some other indicator) since the
target was not found in any of the rows.



 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
 [1,]00110
 [2,]10000
 [3,]10000
 [4,]11000
 [5,]11100
 [6,]00110
 [7,]01110
 [8,]00110
 [9,]11011
[10,]10100

target:  1 1 0 1 1

Should return 9 since the target was  found in row 9


If the target is found, it is no longer necessary to keep
searching the rest of the matrix (which may be quite large)

The data/size etc may change of course, but target will
always have the same number of columns as the matrix.

I tried variations of which, and a for loop
comparing pop[i,] to target without much success, nor
did google yield any results. I am hoping someone here
can provide a suggestion.

Thanks,

EB


-

# Here is the code that generates the above data

create_bin_string - function(len)
{
  sample(0:1, len, replace=T)
}

ROWS = 10
COLS =  5
pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)



target=c(1, 1, 0, 1, 1)

# my population
print(pop)

# I am looking for the index of this in pop
# if present (else -1?)
cat(\ntarget: , target, \n)



##
## this is NOT working
## plus it would continue to search
## after it found the target
##
for(i in ROWS)
   if (pop[i,] == target)
  cat(\nfound in row: , i, \n\n)

__
R-help@r-project.org 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.




Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@r-project.org 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.


Re: [R] searching for specific row in matrix

2008-06-11 Thread Henrique Dallazuanna
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 where target is found.

 Example:

  source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
  [1,]10110
  [2,]11010
  [3,]00100
  [4,]10011
  [5,]10111
  [6,]11001
  [7,]10011
  [8,]00111
  [9,]01101
 [10,]00010

 target:  1 1 0 1 1

 Should return -1 (or some other indicator) since the
 target was not found in any of the rows.



  source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
  [1,]00110
  [2,]10000
  [3,]10000
  [4,]11000
  [5,]11100
  [6,]00110
  [7,]01110
  [8,]00110
  [9,]11011
 [10,]10100

 target:  1 1 0 1 1

 Should return 9 since the target was  found in row 9


 If the target is found, it is no longer necessary to keep
 searching the rest of the matrix (which may be quite large)

 The data/size etc may change of course, but target will
 always have the same number of columns as the matrix.

 I tried variations of which, and a for loop
 comparing pop[i,] to target without much success, nor
 did google yield any results. I am hoping someone here
 can provide a suggestion.

 Thanks,

 EB


 -

 # Here is the code that generates the above data

 create_bin_string - function(len)
 {
  sample(0:1, len, replace=T)
 }

 ROWS = 10
 COLS =  5
 pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)



 target=c(1, 1, 0, 1, 1)

 # my population
 print(pop)

 # I am looking for the index of this in pop
 # if present (else -1?)
 cat(\ntarget: , target, \n)



 ##
 ## this is NOT working
 ## plus it would continue to search
 ## after it found the target
 ##
 for(i in ROWS)
   if (pop[i,] == target)
  cat(\nfound in row: , i, \n\n)

 __
 R-help@r-project.org 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

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 stopping when it finds a match may slow it down?

This is my own solution I came up with in the meantime,
looks rather pedestrian compared to your one line, but it will
drop out immediately once if finds the target. Yours looks (based
simply on appearance :-) faster. Having no feel for the language
I may just have to time them.


I would assume that your solution would be faster simple since it's
using built-in language constructs which are optimized (and implemented
in C?) instead of my own interpreted way.


# return index of target in pop, else -1
searchPop - function(pop, target)
{
rows = length(pop[1,])
for(i in 1:rows)
{
result = (pop[i,] == target)
if (sum(which(result==FALSE)) == 0)
return(i)
}

return (-1)
}


idx=searchPop(pop, target)

if (idx  0)
{
cat(NOT found\n)
} else
cat(Found at position , idx, \n)



Esmail

--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O


__
R-help@r-project.org 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.


Re: [R] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

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 teeth in :-)  My own solution
doesn't seem to work quite correctly as I found out from some
further testing .. so the solutions posted here are much appreciated!

Esmail

__
R-help@r-project.org 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.


Re: [R] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian



# 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

__
R-help@r-project.org 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.