Re: [R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread Jeff Newmiller
Since both of you seem to have misinterpreted my response, consider the 
following for clarification:



A <- matrix(1:1000, 1000, 10)
B <- A[1:100, ]
# my recommended solution
t1 <- system.time({match(as.data.frame(t(B)), as.data.frame(t(A)))})
# similar to John's recommended solution
t2 <- system.time({

+   AA <- as.list(as.data.frame(t(A)))
+   BB <- as.list(as.data.frame(t(B)))
+   which( AA %in% BB )
+ })

t3 <- system.time({

+   lresult <- rep( NA, nrow(A) )
+   for ( ia in seq.int( nrow( A ) ) ) {
+ lres <- FALSE
+ ib <- 0
+ while ( ib < nrow( B ) & !lres ) {
+   ib <- ib + 1
+   lres <- all( A[ ia, ] == B[ ib, ] )
+ }
+ lresult[ ia ] <- lres
+   }
+   which( lresult )
+ })

t4 <- system.time({

+   res<-c()
+   rowsB = length(B[,1])
+   rowsA = length(A[,1])
+   colsB = length(B[1,])
+   colsA = length(A[1,])
+   for (i in 1:rowsB){
+ for (j in 1:colsB){
+   for (k in 1:rowsA){
+ for (l in 1:colsA){
+   if(A[k,l]==B[i,j]){res<-c(res,k)}
+ }
+   }
+ }
+   }
+   unique(sort(res))
+ })

t1

   user  system elapsed
  0.022   0.000   0.020

t2

   user  system elapsed
   0.020.000.02

t3

   user  system elapsed
  0.748   0.000   0.746

t4

   user  system elapsed
 16.612   0.016  16.636
# data.frames are lists, but applying as.list seems to speed up the 
# match for some reason

t2[1]/t1[1]

user.self
0.9090909

# intended comparison for learning purposes
t4[1]/t3[1]

user.self
 22.20856

I recognize that the reference implementation does not need to be 
optimized, but the changes I suggested to it illustrate an incremental 
improvement toward "thinking in R" rather than the optimal solution.


On Fri, 31 Oct 2014, John Fox wrote:


Dear Jeff,

For curiosity, I compared your solution with the one I posted earlier this 
morning (when I was working on a slower computer, accounting for the somewhat 
different timings for my solution):

 snip --


A <- matrix(1:1, 1, 10)
B <- A[1:1000, ]

system.time({

+AA <- as.list(as.data.frame(t(A)))
+BB <- as.list(as.data.frame(t(B)))
+print(sum(AA %in% BB))
+  })
[1] 1000
  user  system elapsed
  0.140.010.16



system.time({

+ lresult <- rep( NA, nrow(A) )
+ for ( ia in seq.int( nrow( A ) ) ) {
+ lres <- FALSE
+ ib <- 0
+ while ( ib < nrow( B ) & !lres ) {
+ ib <- ib + 1
+ lres <- all( A[ ia, ] == B[ ib, ] )
+ }
+ lresult[ ia ] <- lres
+ }
+ print(sum( lresult ))
+ })
[1] 1000
  user  system elapsed
 45.760.01   45.77

46/0.16

[1] 287.5

 snip --

So the solution using nested loops is more than 2 orders of magnitude slower 
for this problem. Of course, for a one-off problem, depending on its size, the 
difference may not matter.

Best,
John

---
John Fox, Professor
McMaster University
Hamilton, Ontario, Canada
http://socserv.socsci.mcmaster.ca/jfox/




-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
project.org] On Behalf Of Jeff Newmiller
Sent: Friday, October 31, 2014 10:15 AM
To: Charles Novaes de Santana; r-help@r-project.org
Subject: Re: [R] Comparing matrices in R - matrixB %in% matrixA

Thank you for the reproducible example, but posting in HTML can corrupt
your example code so please learn to set your email client mail format
appropriately when posting to this list.

I think this [1] post, found with a quick Google search for "R match
matrix", fits your situation perfectly.

match(data.frame(t(B)), data.frame(t(A)))

Note that concatenating vectors in loops is bad news... a basic
optimization for your code would be to preallocate a logical result
vector and fill in each element with a TRUE/FALSE in the outer loop,
and use the which() function on that completed vector to identify the
index numbers (if you really need that). For example:

lresult <- rep( NA, nrow(A) )
for ( ia in seq.int( nrow( A ) ) ) {
  lres <- FALSE
  ib <- 0
  while ( ib < nrow( B ) & !lres ) {
ib <- ib + 1
lres <- all( A[ ia, ] == B[ ib, ] )
  }
  lresult[ ia ] <- lres
}
result <- which( lresult )

[1] http://stackoverflow.com/questions/12697122/in-r-match-function-
for-rows-or-columns-of-matrix
---

Jeff NewmillerThe .   .  Go
Live...
DCN:Basics: ##.#.   ##.#.  Live
Go...
  Live:   OO#.. Dead: OO#..
Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.
rocks...1k
---

Sent from my phone. Please excuse my br

Re: [R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread John Fox
Dear Jeff,

For curiosity, I compared your solution with the one I posted earlier this 
morning (when I was working on a slower computer, accounting for the somewhat 
different timings for my solution):

 snip --

> A <- matrix(1:1, 1, 10)
> B <- A[1:1000, ]
> 
> system.time({
+AA <- as.list(as.data.frame(t(A)))
+BB <- as.list(as.data.frame(t(B)))
+print(sum(AA %in% BB))
+  })
[1] 1000
   user  system elapsed 
   0.140.010.16 
> 
> 
> system.time({
+ lresult <- rep( NA, nrow(A) )
+ for ( ia in seq.int( nrow( A ) ) ) {
+ lres <- FALSE
+ ib <- 0
+ while ( ib < nrow( B ) & !lres ) {
+ ib <- ib + 1
+ lres <- all( A[ ia, ] == B[ ib, ] )
+ }
+ lresult[ ia ] <- lres
+ }
+ print(sum( lresult ))
+ })
[1] 1000
   user  system elapsed 
  45.760.01   45.77 
> 46/0.16
[1] 287.5

 snip --

So the solution using nested loops is more than 2 orders of magnitude slower 
for this problem. Of course, for a one-off problem, depending on its size, the 
difference may not matter.

Best,
 John

---
John Fox, Professor
McMaster University
Hamilton, Ontario, Canada
http://socserv.socsci.mcmaster.ca/jfox/



> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Jeff Newmiller
> Sent: Friday, October 31, 2014 10:15 AM
> To: Charles Novaes de Santana; r-help@r-project.org
> Subject: Re: [R] Comparing matrices in R - matrixB %in% matrixA
> 
> Thank you for the reproducible example, but posting in HTML can corrupt
> your example code so please learn to set your email client mail format
> appropriately when posting to this list.
> 
> I think this [1] post, found with a quick Google search for "R match
> matrix", fits your situation perfectly.
> 
> match(data.frame(t(B)), data.frame(t(A)))
> 
> Note that concatenating vectors in loops is bad news... a basic
> optimization for your code would be to preallocate a logical result
> vector and fill in each element with a TRUE/FALSE in the outer loop,
> and use the which() function on that completed vector to identify the
> index numbers (if you really need that). For example:
> 
> lresult <- rep( NA, nrow(A) )
> for ( ia in seq.int( nrow( A ) ) ) {
>   lres <- FALSE
>   ib <- 0
>   while ( ib < nrow( B ) & !lres ) {
> ib <- ib + 1
> lres <- all( A[ ia, ] == B[ ib, ] )
>   }
>   lresult[ ia ] <- lres
> }
> result <- which( lresult )
> 
> [1] http://stackoverflow.com/questions/12697122/in-r-match-function-
> for-rows-or-columns-of-matrix
> ---
> 
> Jeff NewmillerThe .   .  Go
> Live...
> DCN:Basics: ##.#.   ##.#.  Live
> Go...
>   Live:   OO#.. Dead: OO#..
> Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.
> rocks...1k
> ---
> 
> Sent from my phone. Please excuse my brevity.
> 
> On October 31, 2014 6:20:38 AM PDT, Charles Novaes de Santana
>  wrote:
> >My apologies, because I sent the message before finishing it. i am
> very
> >sorry about this. Please find below my message (I use to write the
> >messages
> >from the end to the beginning... sorry :)).
> >
> >Dear all,
> >
> >I am trying to compare two matrices, in order to find in which rows of
> >a
> >matrix A I can find the same values as in matrix B. I am trying to do
> >it
> >for matrices with around 2500 elements, but please find below a toy
> >example:
> >
> >A = matrix(1:10,nrow=5)
> >B = A[-c(1,2,3),];
> >
> >So
> >> A
> > [,1] [,2]
> >[1,]16
> >[2,]27
> >[3,]38
> >[4,]49
> >[5,]5   10
> >
> >and
> >> B
> > [,1] [,2]
> >[1,]49
> >[2,]5   10
> >
> >I would like to compare A and B in order to find in which rows of A I
> >can
> >find the  rows of B. Something similar to %in% with one dimensional
> >arrays.
> >In the example above, the answer should be 4 and 5.
> >
> >I did a function to do it (see it below), it gives me the correct
> >answer
> >for this toy example, but the excess of for-loops makes it extremely
> >slow
> >for larger matrices. I was wondering if there is a better w

Re: [R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread Charles Novaes de Santana
Thank you, Jeff, for your message.

I did a search, but maybe my problem was that I didn't know the correct way
to search my problem (in other words: my vocabulary in R/English  is not
good). Because of this I choose to send a message to the list the most
detailed as possible, and with a first solution, that was not the optimum
one.

Thank you very much for taking your time to think about my problem and to
search for a good solution! I much appreciate it! But I think John'
suggestion does what I need and it is much simpler :)

Best,

Charles
P.S.: the HTML issue is probably because I am using a webmail-client and I
did a copy and paste from the first email I sent.

On Fri, Oct 31, 2014 at 3:15 PM, Jeff Newmiller 
wrote:

> Thank you for the reproducible example, but posting in HTML can corrupt
> your example code so please learn to set your email client mail format
> appropriately when posting to this list.
>
> I think this [1] post, found with a quick Google search for "R match
> matrix", fits your situation perfectly.
>
> match(data.frame(t(B)), data.frame(t(A)))
>
> Note that concatenating vectors in loops is bad news... a basic
> optimization for your code would be to preallocate a logical result vector
> and fill in each element with a TRUE/FALSE in the outer loop, and use the
> which() function on that completed vector to identify the index numbers (if
> you really need that). For example:
>
> lresult <- rep( NA, nrow(A) )
> for ( ia in seq.int( nrow( A ) ) ) {
>   lres <- FALSE
>   ib <- 0
>   while ( ib < nrow( B ) & !lres ) {
> ib <- ib + 1
> lres <- all( A[ ia, ] == B[ ib, ] )
>   }
>   lresult[ ia ] <- lres
> }
> result <- which( lresult )
>
> [1]
> http://stackoverflow.com/questions/12697122/in-r-match-function-for-rows-or-columns-of-matrix
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live
> Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> ---
> Sent from my phone. Please excuse my brevity.
>
> On October 31, 2014 6:20:38 AM PDT, Charles Novaes de Santana <
> charles.sant...@gmail.com> wrote:
> >My apologies, because I sent the message before finishing it. i am very
> >sorry about this. Please find below my message (I use to write the
> >messages
> >from the end to the beginning... sorry :)).
> >
> >Dear all,
> >
> >I am trying to compare two matrices, in order to find in which rows of
> >a
> >matrix A I can find the same values as in matrix B. I am trying to do
> >it
> >for matrices with around 2500 elements, but please find below a toy
> >example:
> >
> >A = matrix(1:10,nrow=5)
> >B = A[-c(1,2,3),];
> >
> >So
> >> A
> > [,1] [,2]
> >[1,]16
> >[2,]27
> >[3,]38
> >[4,]49
> >[5,]5   10
> >
> >and
> >> B
> > [,1] [,2]
> >[1,]49
> >[2,]5   10
> >
> >I would like to compare A and B in order to find in which rows of A I
> >can
> >find the  rows of B. Something similar to %in% with one dimensional
> >arrays.
> >In the example above, the answer should be 4 and 5.
> >
> >I did a function to do it (see it below), it gives me the correct
> >answer
> >for this toy example, but the excess of for-loops makes it extremely
> >slow
> >for larger matrices. I was wondering if there is a better way to do
> >this
> >kind of comparison. Any idea? Sorry if it is a stupid question.
> >
> >matbinmata<-function(B,A){
> >res<-c();
> >rowsB = length(B[,1]);
> >rowsA = length(A[,1]);
> >colsB = length(B[1,]);
> >colsA = length(A[1,]);
> >for (i in 1:rowsB){
> >for (j in 1:colsB){
> >for (k in 1:rowsA){
> >for (l in 1:colsA){
> >if(A[k,l]==B[i,j]){res<-c(res,k);}
> >}
> >}
> >}
> >}
> >return(unique(sort(res)));
> >}
> >
> >
> >Best,
> >
> >Charles
> >
> >On Fri, Oct 31, 2014 at 2:12 PM, Charles Novaes de Santana <
> >charles.sant...@gmail.com> wrote:
> >
> >> A = matrix(1:10,nrow=5)
> >> B = A[-c(1,2,3),];
> >>
> >> So
> >> > A
> >>  [,1] [,2]
> >> [1,]16
> >> [2,]27
> >> [3,]38
> >> [4,]49
> >> [5,]5   10
> >>
> >> and
> >> > B
> >>  [,1] [,2]
> >> [1,]49
> >> [2,]5   10
> >>
> >> I would like to compare A and B in order to find in which rows of A I
> >can
> >> find the  rows of B. Something similar to %in% with one dimensional
> >arrays.
> >> In the example above, the answer should be 4 and 5.
> >>
> >> I did a function to do it (see it below), it gives me the correct
> >answer
> >> for this toy example, but the excess of for-loops makes it extremely
> >slow
> >> for larger matric

Re: [R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread Jeff Newmiller
Thank you for the reproducible example, but posting in HTML can corrupt your 
example code so please learn to set your email client mail format appropriately 
when posting to this list.

I think this [1] post, found with a quick Google search for "R match matrix", 
fits your situation perfectly.

match(data.frame(t(B)), data.frame(t(A)))

Note that concatenating vectors in loops is bad news... a basic optimization 
for your code would be to preallocate a logical result vector and fill in each 
element with a TRUE/FALSE in the outer loop, and use the which() function on 
that completed vector to identify the index numbers (if you really need that). 
For example:

lresult <- rep( NA, nrow(A) )
for ( ia in seq.int( nrow( A ) ) ) {
  lres <- FALSE
  ib <- 0
  while ( ib < nrow( B ) & !lres ) {
ib <- ib + 1
lres <- all( A[ ia, ] == B[ ib, ] )
  }
  lresult[ ia ] <- lres
}
result <- which( lresult )

[1] 
http://stackoverflow.com/questions/12697122/in-r-match-function-for-rows-or-columns-of-matrix
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On October 31, 2014 6:20:38 AM PDT, Charles Novaes de Santana 
 wrote:
>My apologies, because I sent the message before finishing it. i am very
>sorry about this. Please find below my message (I use to write the
>messages
>from the end to the beginning... sorry :)).
>
>Dear all,
>
>I am trying to compare two matrices, in order to find in which rows of
>a
>matrix A I can find the same values as in matrix B. I am trying to do
>it
>for matrices with around 2500 elements, but please find below a toy
>example:
>
>A = matrix(1:10,nrow=5)
>B = A[-c(1,2,3),];
>
>So
>> A
> [,1] [,2]
>[1,]16
>[2,]27
>[3,]38
>[4,]49
>[5,]5   10
>
>and
>> B
> [,1] [,2]
>[1,]49
>[2,]5   10
>
>I would like to compare A and B in order to find in which rows of A I
>can
>find the  rows of B. Something similar to %in% with one dimensional
>arrays.
>In the example above, the answer should be 4 and 5.
>
>I did a function to do it (see it below), it gives me the correct
>answer
>for this toy example, but the excess of for-loops makes it extremely
>slow
>for larger matrices. I was wondering if there is a better way to do
>this
>kind of comparison. Any idea? Sorry if it is a stupid question.
>
>matbinmata<-function(B,A){
>res<-c();
>rowsB = length(B[,1]);
>rowsA = length(A[,1]);
>colsB = length(B[1,]);
>colsA = length(A[1,]);
>for (i in 1:rowsB){
>for (j in 1:colsB){
>for (k in 1:rowsA){
>for (l in 1:colsA){
>if(A[k,l]==B[i,j]){res<-c(res,k);}
>}
>}
>}
>}
>return(unique(sort(res)));
>}
>
>
>Best,
>
>Charles
>
>On Fri, Oct 31, 2014 at 2:12 PM, Charles Novaes de Santana <
>charles.sant...@gmail.com> wrote:
>
>> A = matrix(1:10,nrow=5)
>> B = A[-c(1,2,3),];
>>
>> So
>> > A
>>  [,1] [,2]
>> [1,]16
>> [2,]27
>> [3,]38
>> [4,]49
>> [5,]5   10
>>
>> and
>> > B
>>  [,1] [,2]
>> [1,]49
>> [2,]5   10
>>
>> I would like to compare A and B in order to find in which rows of A I
>can
>> find the  rows of B. Something similar to %in% with one dimensional
>arrays.
>> In the example above, the answer should be 4 and 5.
>>
>> I did a function to do it (see it below), it gives me the correct
>answer
>> for this toy example, but the excess of for-loops makes it extremely
>slow
>> for larger matrices. I was wondering if there is a better way to do
>this
>> kind of comparison. Any idea? Sorry if it is a stupid question.
>>
>> matbinmata<-function(B,A){
>> res<-c();
>> rowsB = length(B[,1]);
>> rowsA = length(A[,1]);
>> colsB = length(B[1,]);
>> colsA = length(A[1,]);
>> for (i in 1:rowsB){
>> for (j in 1:colsB){
>> for (k in 1:rowsA){
>> for (l in 1:colsA){
>> if(A[k,l]==B[i,j]){res<-c(res,k);}
>> }
>> }
>> }
>> }
>> return(unique(sort(res)));
>> }
>>
>>
>> Best,
>>
>> Charles
>>
>>
>> --
>> Um axé! :)
>>
>> --
>> Charles Novaes de Santana, PhD
>> http://www.imedea.uib-csic.es/~charles
>>
>
>
>
>-- 
>Um axé! :)
>
>--
>Charles Novaes de Santana, PhD
>http://www.imedea.uib-csic.es/~charles
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the po

Re: [R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread Charles Novaes de Santana
Great!! It is perfect!

Thank you, John, for this elegant and fast suggestion!

Best,

Charles

On Fri, Oct 31, 2014 at 2:35 PM, John Fox  wrote:

> Dear Charles,
>
> How about the following?
>
> --- snip -
>
> > AA <- as.list(as.data.frame(t(A)))
> > BB <- as.list(as.data.frame(t(B)))
> > which(AA %in% BB)
> [1] 4 5
>
> --- snip -
>
> This seems reasonably fast. For example:
>
> --- snip -
>
> > A <- matrix(1:1, 1, 10)
> > B <- A[1:1000, ]
> >
> > system.time({
> +   AA <- as.list(as.data.frame(t(A)))
> +   BB <- as.list(as.data.frame(t(B)))
> +   print(sum(AA %in% BB))
> + })
> [1] 1000
>user  system elapsed
>0.260.000.26
>
> --- snip -
>
> I hope this helps,
>  John
>
> 
> John Fox, Professor
> McMaster University
> Hamilton, Ontario, Canada
> http://socserv.mcmaster.ca/jfox/
>
>
>
>
> On Fri, 31 Oct 2014 14:20:38 +0100
>  Charles Novaes de Santana  wrote:
> > My apologies, because I sent the message before finishing it. i am very
> > sorry about this. Please find below my message (I use to write the
> messages
> > from the end to the beginning... sorry :)).
> >
> > Dear all,
> >
> > I am trying to compare two matrices, in order to find in which rows of a
> > matrix A I can find the same values as in matrix B. I am trying to do it
> > for matrices with around 2500 elements, but please find below a toy
> example:
> >
> > A = matrix(1:10,nrow=5)
> > B = A[-c(1,2,3),];
> >
> > So
> > > A
> >  [,1] [,2]
> > [1,]16
> > [2,]27
> > [3,]38
> > [4,]49
> > [5,]5   10
> >
> > and
> > > B
> >  [,1] [,2]
> > [1,]49
> > [2,]5   10
> >
> > I would like to compare A and B in order to find in which rows of A I can
> > find the  rows of B. Something similar to %in% with one dimensional
> arrays.
> > In the example above, the answer should be 4 and 5.
> >
> > I did a function to do it (see it below), it gives me the correct answer
> > for this toy example, but the excess of for-loops makes it extremely slow
> > for larger matrices. I was wondering if there is a better way to do this
> > kind of comparison. Any idea? Sorry if it is a stupid question.
> >
> > matbinmata<-function(B,A){
> > res<-c();
> > rowsB = length(B[,1]);
> > rowsA = length(A[,1]);
> > colsB = length(B[1,]);
> > colsA = length(A[1,]);
> > for (i in 1:rowsB){
> > for (j in 1:colsB){
> > for (k in 1:rowsA){
> > for (l in 1:colsA){
> > if(A[k,l]==B[i,j]){res<-c(res,k);}
> > }
> > }
> > }
> > }
> > return(unique(sort(res)));
> > }
> >
> >
> > Best,
> >
> > Charles
> >
> > On Fri, Oct 31, 2014 at 2:12 PM, Charles Novaes de Santana <
> > charles.sant...@gmail.com> wrote:
> >
> > > A = matrix(1:10,nrow=5)
> > > B = A[-c(1,2,3),];
> > >
> > > So
> > > > A
> > >  [,1] [,2]
> > > [1,]16
> > > [2,]27
> > > [3,]38
> > > [4,]49
> > > [5,]5   10
> > >
> > > and
> > > > B
> > >  [,1] [,2]
> > > [1,]49
> > > [2,]5   10
> > >
> > > I would like to compare A and B in order to find in which rows of A I
> can
> > > find the  rows of B. Something similar to %in% with one dimensional
> arrays.
> > > In the example above, the answer should be 4 and 5.
> > >
> > > I did a function to do it (see it below), it gives me the correct
> answer
> > > for this toy example, but the excess of for-loops makes it extremely
> slow
> > > for larger matrices. I was wondering if there is a better way to do
> this
> > > kind of comparison. Any idea? Sorry if it is a stupid question.
> > >
> > > matbinmata<-function(B,A){
> > > res<-c();
> > > rowsB = length(B[,1]);
> > > rowsA = length(A[,1]);
> > > colsB = length(B[1,]);
> > > colsA = length(A[1,]);
> > > for (i in 1:rowsB){
> > > for (j in 1:colsB){
> > > for (k in 1:rowsA){
> > > for (l in 1:colsA){
> > > if(A[k,l]==B[i,j]){res<-c(res,k);}
> > > }
> > > }
> > > }
> > > }
> > > return(unique(sort(res)));
> > > }
> > >
> > >
> > > Best,
> > >
> > > Charles
> > >
> > >
> > > --
> > > Um axé! :)
> > >
> > > --
> > > Charles Novaes de Santana, PhD
> > > http://www.imedea.uib-csic.es/~charles
> > >
> >
> >
> >
> > --
> > Um axé! :)
> >
> > --
> > Charles Novaes de Santana, PhD
> > http://www.imedea.uib-csic.es/~charles
> >
> >   [[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.
>
>


-- 
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

  

Re: [R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread John Fox
Dear Charles,

How about the following?

--- snip -

> AA <- as.list(as.data.frame(t(A)))
> BB <- as.list(as.data.frame(t(B)))
> which(AA %in% BB)
[1] 4 5

--- snip -

This seems reasonably fast. For example:

--- snip -

> A <- matrix(1:1, 1, 10)
> B <- A[1:1000, ]
> 
> system.time({
+   AA <- as.list(as.data.frame(t(A)))
+   BB <- as.list(as.data.frame(t(B)))
+   print(sum(AA %in% BB))
+ })
[1] 1000
   user  system elapsed 
   0.260.000.26 

--- snip -

I hope this helps,
 John


John Fox, Professor
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/




On Fri, 31 Oct 2014 14:20:38 +0100
 Charles Novaes de Santana  wrote:
> My apologies, because I sent the message before finishing it. i am very
> sorry about this. Please find below my message (I use to write the messages
> from the end to the beginning... sorry :)).
> 
> Dear all,
> 
> I am trying to compare two matrices, in order to find in which rows of a
> matrix A I can find the same values as in matrix B. I am trying to do it
> for matrices with around 2500 elements, but please find below a toy example:
> 
> A = matrix(1:10,nrow=5)
> B = A[-c(1,2,3),];
> 
> So
> > A
>  [,1] [,2]
> [1,]16
> [2,]27
> [3,]38
> [4,]49
> [5,]5   10
> 
> and
> > B
>  [,1] [,2]
> [1,]49
> [2,]5   10
> 
> I would like to compare A and B in order to find in which rows of A I can
> find the  rows of B. Something similar to %in% with one dimensional arrays.
> In the example above, the answer should be 4 and 5.
> 
> I did a function to do it (see it below), it gives me the correct answer
> for this toy example, but the excess of for-loops makes it extremely slow
> for larger matrices. I was wondering if there is a better way to do this
> kind of comparison. Any idea? Sorry if it is a stupid question.
> 
> matbinmata<-function(B,A){
> res<-c();
> rowsB = length(B[,1]);
> rowsA = length(A[,1]);
> colsB = length(B[1,]);
> colsA = length(A[1,]);
> for (i in 1:rowsB){
> for (j in 1:colsB){
> for (k in 1:rowsA){
> for (l in 1:colsA){
> if(A[k,l]==B[i,j]){res<-c(res,k);}
> }
> }
> }
> }
> return(unique(sort(res)));
> }
> 
> 
> Best,
> 
> Charles
> 
> On Fri, Oct 31, 2014 at 2:12 PM, Charles Novaes de Santana <
> charles.sant...@gmail.com> wrote:
> 
> > A = matrix(1:10,nrow=5)
> > B = A[-c(1,2,3),];
> >
> > So
> > > A
> >  [,1] [,2]
> > [1,]16
> > [2,]27
> > [3,]38
> > [4,]49
> > [5,]5   10
> >
> > and
> > > B
> >  [,1] [,2]
> > [1,]49
> > [2,]5   10
> >
> > I would like to compare A and B in order to find in which rows of A I can
> > find the  rows of B. Something similar to %in% with one dimensional arrays.
> > In the example above, the answer should be 4 and 5.
> >
> > I did a function to do it (see it below), it gives me the correct answer
> > for this toy example, but the excess of for-loops makes it extremely slow
> > for larger matrices. I was wondering if there is a better way to do this
> > kind of comparison. Any idea? Sorry if it is a stupid question.
> >
> > matbinmata<-function(B,A){
> > res<-c();
> > rowsB = length(B[,1]);
> > rowsA = length(A[,1]);
> > colsB = length(B[1,]);
> > colsA = length(A[1,]);
> > for (i in 1:rowsB){
> > for (j in 1:colsB){
> > for (k in 1:rowsA){
> > for (l in 1:colsA){
> > if(A[k,l]==B[i,j]){res<-c(res,k);}
> > }
> > }
> > }
> > }
> > return(unique(sort(res)));
> > }
> >
> >
> > Best,
> >
> > Charles
> >
> >
> > --
> > Um axé! :)
> >
> > --
> > Charles Novaes de Santana, PhD
> > http://www.imedea.uib-csic.es/~charles
> >
> 
> 
> 
> -- 
> Um axé! :)
> 
> --
> Charles Novaes de Santana, PhD
> http://www.imedea.uib-csic.es/~charles
> 
>   [[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.

__
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] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread Charles Novaes de Santana
My apologies, because I sent the message before finishing it. i am very
sorry about this. Please find below my message (I use to write the messages
from the end to the beginning... sorry :)).

Dear all,

I am trying to compare two matrices, in order to find in which rows of a
matrix A I can find the same values as in matrix B. I am trying to do it
for matrices with around 2500 elements, but please find below a toy example:

A = matrix(1:10,nrow=5)
B = A[-c(1,2,3),];

So
> A
 [,1] [,2]
[1,]16
[2,]27
[3,]38
[4,]49
[5,]5   10

and
> B
 [,1] [,2]
[1,]49
[2,]5   10

I would like to compare A and B in order to find in which rows of A I can
find the  rows of B. Something similar to %in% with one dimensional arrays.
In the example above, the answer should be 4 and 5.

I did a function to do it (see it below), it gives me the correct answer
for this toy example, but the excess of for-loops makes it extremely slow
for larger matrices. I was wondering if there is a better way to do this
kind of comparison. Any idea? Sorry if it is a stupid question.

matbinmata<-function(B,A){
res<-c();
rowsB = length(B[,1]);
rowsA = length(A[,1]);
colsB = length(B[1,]);
colsA = length(A[1,]);
for (i in 1:rowsB){
for (j in 1:colsB){
for (k in 1:rowsA){
for (l in 1:colsA){
if(A[k,l]==B[i,j]){res<-c(res,k);}
}
}
}
}
return(unique(sort(res)));
}


Best,

Charles

On Fri, Oct 31, 2014 at 2:12 PM, Charles Novaes de Santana <
charles.sant...@gmail.com> wrote:

> A = matrix(1:10,nrow=5)
> B = A[-c(1,2,3),];
>
> So
> > A
>  [,1] [,2]
> [1,]16
> [2,]27
> [3,]38
> [4,]49
> [5,]5   10
>
> and
> > B
>  [,1] [,2]
> [1,]49
> [2,]5   10
>
> I would like to compare A and B in order to find in which rows of A I can
> find the  rows of B. Something similar to %in% with one dimensional arrays.
> In the example above, the answer should be 4 and 5.
>
> I did a function to do it (see it below), it gives me the correct answer
> for this toy example, but the excess of for-loops makes it extremely slow
> for larger matrices. I was wondering if there is a better way to do this
> kind of comparison. Any idea? Sorry if it is a stupid question.
>
> matbinmata<-function(B,A){
> res<-c();
> rowsB = length(B[,1]);
> rowsA = length(A[,1]);
> colsB = length(B[1,]);
> colsA = length(A[1,]);
> for (i in 1:rowsB){
> for (j in 1:colsB){
> for (k in 1:rowsA){
> for (l in 1:colsA){
> if(A[k,l]==B[i,j]){res<-c(res,k);}
> }
> }
> }
> }
> return(unique(sort(res)));
> }
>
>
> Best,
>
> Charles
>
>
> --
> Um axé! :)
>
> --
> Charles Novaes de Santana, PhD
> http://www.imedea.uib-csic.es/~charles
>



-- 
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

[[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.


[R] Comparing matrices in R - matrixB %in% matrixA

2014-10-31 Thread Charles Novaes de Santana
A = matrix(1:10,nrow=5)
B = A[-c(1,2,3),];

So
> A
 [,1] [,2]
[1,]16
[2,]27
[3,]38
[4,]49
[5,]5   10

and
> B
 [,1] [,2]
[1,]49
[2,]5   10

I would like to compare A and B in order to find in which rows of A I can
find the  rows of B. Something similar to %in% with one dimensional arrays.
In the example above, the answer should be 4 and 5.

I did a function to do it (see it below), it gives me the correct answer
for this toy example, but the excess of for-loops makes it extremely slow
for larger matrices. I was wondering if there is a better way to do this
kind of comparison. Any idea? Sorry if it is a stupid question.

matbinmata<-function(B,A){
res<-c();
rowsB = length(B[,1]);
rowsA = length(A[,1]);
colsB = length(B[1,]);
colsA = length(A[1,]);
for (i in 1:rowsB){
for (j in 1:colsB){
for (k in 1:rowsA){
for (l in 1:colsA){
if(A[k,l]==B[i,j]){res<-c(res,k);}
}
}
}
}
return(unique(sort(res)));
}


Best,

Charles


-- 
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

[[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] Comparing matrices

2010-03-11 Thread Miguel Porto
Also look at

?any

and

?all

Very handy functions.

Miguel


On Thu, Mar 11, 2010 at 12:37 PM, Esmail  wrote:

> Hello all,
>
> I have two matrices, pop and pop2, each the same number of rows and
> columns that I want to compare for equality. I am concerned about
> efficiency in this operation.
>
> I've tried a few things without success so far. Doing something simple
> like:
>
> if (pop==pop2) { cat('equal') } else { cat('NOT equal') }
>
> results in the warning:
> 1: In if (pop == pop2) { :
>  the condition has length > 1 and only the first element will be used
>
> so it seems to look only at the first element.
>
> print(pop==pop2) gives me a listing of TRUE/FALSE values for each
> element.
>
> I am really only looking for a single TRUE or FALSE value to tell me
> if the two populations (ie pop and pop2) are the same or different. In
> my application there will be about 200 columns and 50 rows and this
> comparison will happen very frequently (possibly a few thousand
> times), so I am concerned about efficiency.
>
> I am appending the code I used to generate my matrices and some things
> I tried (mentioned above) and also the output get so far.
>
> FWIW, Linux environment, R 2.10.1.
>
> Thanks,
> Esmail
>
> ps: Am I correct that if I do the assignment
>
>pop2 = pop
>
>I create a totally separate instance/(deep)copy of the
>data? I tried a few tests that seem to confirm this, but I'd
>rather be sure.
>
>
> --- code 
>
> # create a binary vector of size "len"
> create_bin_Chromosome <- function(len)
> {
>  sample(0:1, len, replace=T)
> }
>
> # create popsize members, each of length len
> create_pop_2 <- function(popsize, len)
> {
>  datasize=len*popsize
>
>  npop <- matrix(0, popsize, len, byrow=T)
>
>  for(i in 1:popsize)
>npop[i,] = create_bin_Chromosome(len)
>
>  npop
> }
>
>
> POP_SIZE = 3
> LEN = 8
>
> pop = create_pop_2(POP_SIZE, LEN)
> pop2 = pop
>
> print(pop==pop2)
> if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }
>
> print(pop)
> print(pop2)
>
> pop[2,5] = 99
> pop2[3,3] = 77
>
> print(pop==pop2)
> if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }
>
> print(pop)
> print(pop2)
>
> -- output ---
>
> > source('popc.R')
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> [2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> [3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> equal
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]01010110
> [2,]01100000
> [3,]01001011
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]01010110
> [2,]01100000
> [3,]01001011
> [,1] [,2]  [,3] [,4]  [,5] [,6] [,7] [,8]
> [1,] TRUE TRUE  TRUE TRUE  TRUE TRUE TRUE TRUE
> [2,] TRUE TRUE  TRUE TRUE FALSE TRUE TRUE TRUE
> [3,] TRUE TRUE FALSE TRUE  TRUE TRUE TRUE TRUE
> equal
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]01010110
> [2,]0110   99000
> [3,]01001011
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]01010110
> [2,]01100000
> [3,]01   7701011
> Warning messages:
> 1: In if (pop == pop2) { :
>  the condition has length > 1 and only the first element will be used
> 2: In if (pop == pop2) { :
>  the condition has length > 1 and only the first element will be used
> >
>
> __
> 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.
>

[[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] Comparing matrices

2010-03-11 Thread Ravi Varadhan
Look at:

identical(pop, pop2)  # if you want to do "exact" comparison

Or

all.equal(pop, pop2, tolerance=1.e-10)  # if you want to allow for
some small noise

Ravi.

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Esmail
Sent: Thursday, March 11, 2010 7:38 AM
To: r-help@r-project.org
Subject: [R] Comparing matrices

Hello all,

I have two matrices, pop and pop2, each the same number of rows and
columns that I want to compare for equality. I am concerned about
efficiency in this operation.

I've tried a few things without success so far. Doing something simple like:

if (pop==pop2) { cat('equal') } else { cat('NOT equal') }

results in the warning:
1: In if (pop == pop2) { :
   the condition has length > 1 and only the first element will be used

so it seems to look only at the first element.

print(pop==pop2) gives me a listing of TRUE/FALSE values for each
element.

I am really only looking for a single TRUE or FALSE value to tell me
if the two populations (ie pop and pop2) are the same or different. In
my application there will be about 200 columns and 50 rows and this
comparison will happen very frequently (possibly a few thousand
times), so I am concerned about efficiency.

I am appending the code I used to generate my matrices and some things
I tried (mentioned above) and also the output get so far.

FWIW, Linux environment, R 2.10.1.

Thanks,
Esmail

ps: Am I correct that if I do the assignment

 pop2 = pop

 I create a totally separate instance/(deep)copy of the
 data? I tried a few tests that seem to confirm this, but I'd
 rather be sure.


--- code 

# create a binary vector of size "len"
create_bin_Chromosome <- function(len)
{
   sample(0:1, len, replace=T)
}

# create popsize members, each of length len
create_pop_2 <- function(popsize, len)
{
   datasize=len*popsize

   npop <- matrix(0, popsize, len, byrow=T)

   for(i in 1:popsize)
 npop[i,] = create_bin_Chromosome(len)

   npop
}


POP_SIZE = 3
LEN = 8

pop = create_pop_2(POP_SIZE, LEN)
pop2 = pop

print(pop==pop2)
if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }

print(pop)
print(pop2)

pop[2,5] = 99
pop2[3,3] = 77

print(pop==pop2)
if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }

print(pop)
print(pop2)

-- output ---

 > source('popc.R')
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
equal
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01001011
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01001011
  [,1] [,2]  [,3] [,4]  [,5] [,6] [,7] [,8]
[1,] TRUE TRUE  TRUE TRUE  TRUE TRUE TRUE TRUE
[2,] TRUE TRUE  TRUE TRUE FALSE TRUE TRUE TRUE
[3,] TRUE TRUE FALSE TRUE  TRUE TRUE TRUE TRUE
equal
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]0110   99000
[3,]01001011
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01   7701011
Warning messages:
1: In if (pop == pop2) { :
   the condition has length > 1 and only the first element will be used
2: In if (pop == pop2) { :
   the condition has length > 1 and only the first element will be used
 >

__
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.

__
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.


[R] Comparing matrices

2010-03-11 Thread Esmail

Hello all,

I have two matrices, pop and pop2, each the same number of rows and
columns that I want to compare for equality. I am concerned about
efficiency in this operation.

I've tried a few things without success so far. Doing something simple like:

if (pop==pop2) { cat('equal') } else { cat('NOT equal') }

results in the warning:
1: In if (pop == pop2) { :
  the condition has length > 1 and only the first element will be used

so it seems to look only at the first element.

print(pop==pop2) gives me a listing of TRUE/FALSE values for each
element.

I am really only looking for a single TRUE or FALSE value to tell me
if the two populations (ie pop and pop2) are the same or different. In
my application there will be about 200 columns and 50 rows and this
comparison will happen very frequently (possibly a few thousand
times), so I am concerned about efficiency.

I am appending the code I used to generate my matrices and some things
I tried (mentioned above) and also the output get so far.

FWIW, Linux environment, R 2.10.1.

Thanks,
Esmail

ps: Am I correct that if I do the assignment

pop2 = pop

I create a totally separate instance/(deep)copy of the
data? I tried a few tests that seem to confirm this, but I'd
rather be sure.


--- code 

# create a binary vector of size "len"
create_bin_Chromosome <- function(len)
{
  sample(0:1, len, replace=T)
}

# create popsize members, each of length len
create_pop_2 <- function(popsize, len)
{
  datasize=len*popsize

  npop <- matrix(0, popsize, len, byrow=T)

  for(i in 1:popsize)
npop[i,] = create_bin_Chromosome(len)

  npop
}


POP_SIZE = 3
LEN = 8

pop = create_pop_2(POP_SIZE, LEN)
pop2 = pop

print(pop==pop2)
if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }

print(pop)
print(pop2)

pop[2,5] = 99
pop2[3,3] = 77

print(pop==pop2)
if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }

print(pop)
print(pop2)

-- output ---

> source('popc.R')
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
equal
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01001011
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01001011
 [,1] [,2]  [,3] [,4]  [,5] [,6] [,7] [,8]
[1,] TRUE TRUE  TRUE TRUE  TRUE TRUE TRUE TRUE
[2,] TRUE TRUE  TRUE TRUE FALSE TRUE TRUE TRUE
[3,] TRUE TRUE FALSE TRUE  TRUE TRUE TRUE TRUE
equal
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]0110   99000
[3,]01001011
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01   7701011
Warning messages:
1: In if (pop == pop2) { :
  the condition has length > 1 and only the first element will be used
2: In if (pop == pop2) { :
  the condition has length > 1 and only the first element will be used
>

__
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] comparing matrices

2009-04-26 Thread Esmail

ONKELINX, Thierry wrote:

Have a look at all.equal

matA <- matrix(1:4, ncol = 2)
matB <- matA
all.equal(matA, matB)
matB[1,1] <- -10
all.equal(matA, matB)


Hi Thierry,

Thanks, all.equal does indicate if it's all equal so that
works great!

Much nicer than my hack - thanks,

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] comparing matrices

2009-04-26 Thread Esmail

David Winsemius wrote:



 > identical( matrix((1:4), ncol=2), matrix((1:4), nrow=2))
[1] TRUE
 > identical( matrix((1:4), ncol=2), matrix((2:5), nrow=2))
[1] FALSE


Thanks David, 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] comparing matrices

2009-04-26 Thread Esmail

baptiste auguie wrote:

I'm not sure I'm following you but have you tried,

identical(matrix(c(1,1,1,1),ncol=2), matrix(c(1,1,1,1),ncol=2))

?all.equal
?isTRUE
?identical

and possibly the compare package,

compare(matrix(c(1,1,1,1),ncol=2),matrix(c(1,1,1,1),ncol=2))


HTH,

baptiste


Hi Babtiste,

Thanks for pointing out the various options that exist. R is
a very rich language indeed and it's good to know how to accomplish
tasks in various ways.

Cheers,
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] comparing matrices

2009-04-26 Thread David Winsemius


On Apr 26, 2009, at 1:02 PM, Esmail wrote:


I'm trying to compare two matrices made up of bits.

doing a simple comparison of

   matA == matB


> identical( matrix((1:4), ncol=2), matrix((1:4), nrow=2))
[1] TRUE
> identical( matrix((1:4), ncol=2), matrix((2:5), nrow=2))
[1] FALSE




yields this sort of output.

 [,1] [,2]  [,3]  [,4]  [,5]  [,6]
[1,] FALSE TRUE FALSE  TRUE  TRUE FALSE
[2,]  TRUE TRUE  TRUE  TRUE  TRUE  TRUE
[3,] FALSE TRUE FALSE FALSE FALSE  TRUE
[4,] FALSE TRUE  TRUE FALSE FALSE FALSE
[5,]  TRUE TRUE  TRUE  TRUE FALSE FALSE
[6,]  TRUE TRUE  TRUE  TRUE FALSE FALSE

I really would like just one comprehensive value to say TRUE or FALSE.

This is the hack (rather ugly I think) I put together that works,
but there has to be a nicer way, no?

   res=pop[1:ROWS,] == keep[1:ROWS,]


   if ((ROWS*COL) == sum(res))
{
  cat('they are equal\n')
}else
  cat('they are NOT equal\n')


That code is meaningless to us without a definition (in R) of ROWS,   
COL, keep, and pop



Thanks!

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.


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] comparing matrices

2009-04-26 Thread baptiste auguie

I'm not sure I'm following you but have you tried,

identical(matrix(c(1,1,1,1),ncol=2), matrix(c(1,1,1,1),ncol=2))

?all.equal
?isTRUE
?identical

and possibly the compare package,

compare(matrix(c(1,1,1,1),ncol=2),matrix(c(1,1,1,1),ncol=2))


HTH,

baptiste

On 26 Apr 2009, at 18:02, Esmail wrote:


I'm trying to compare two matrices made up of bits.

doing a simple comparison of

matA == matB

yields this sort of output.

  [,1] [,2]  [,3]  [,4]  [,5]  [,6]
[1,] FALSE TRUE FALSE  TRUE  TRUE FALSE
[2,]  TRUE TRUE  TRUE  TRUE  TRUE  TRUE
[3,] FALSE TRUE FALSE FALSE FALSE  TRUE
[4,] FALSE TRUE  TRUE FALSE FALSE FALSE
[5,]  TRUE TRUE  TRUE  TRUE FALSE FALSE
[6,]  TRUE TRUE  TRUE  TRUE FALSE FALSE

I really would like just one comprehensive value to say TRUE or FALSE.

This is the hack (rather ugly I think) I put together that works,
but there has to be a nicer way, no?

res=pop[1:ROWS,] == keep[1:ROWS,]

if ((ROWS*COL) == sum(res))
 {
   cat('they are equal\n')
 }else
   cat('they are NOT equal\n')

Thanks!

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.


_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
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] comparing matrices

2009-04-26 Thread ONKELINX, Thierry
Have a look at all.equal

matA <- matrix(1:4, ncol = 2)
matB <- matA
all.equal(matA, matB)
matB[1,1] <- -10
all.equal(matA, matB)

HTH,

Thierry 




ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
thierry.onkel...@inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-Oorspronkelijk bericht-
Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
Namens Esmail
Verzonden: zondag 26 april 2009 19:03
Aan: R mailing list
Onderwerp: [R] comparing matrices

I'm trying to compare two matrices made up of bits.

doing a simple comparison of

 matA == matB

yields this sort of output.

   [,1] [,2]  [,3]  [,4]  [,5]  [,6]
[1,] FALSE TRUE FALSE  TRUE  TRUE FALSE
[2,]  TRUE TRUE  TRUE  TRUE  TRUE  TRUE
[3,] FALSE TRUE FALSE FALSE FALSE  TRUE
[4,] FALSE TRUE  TRUE FALSE FALSE FALSE
[5,]  TRUE TRUE  TRUE  TRUE FALSE FALSE
[6,]  TRUE TRUE  TRUE  TRUE FALSE FALSE

I really would like just one comprehensive value to say TRUE or FALSE.

This is the hack (rather ugly I think) I put together that works,
but there has to be a nicer way, no?

 res=pop[1:ROWS,] == keep[1:ROWS,]

 if ((ROWS*COL) == sum(res))
  {
cat('they are equal\n')
  }else
cat('they are NOT equal\n')

Thanks!

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.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

__
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.


[R] comparing matrices

2009-04-26 Thread Esmail

I'm trying to compare two matrices made up of bits.

doing a simple comparison of

matA == matB

yields this sort of output.

  [,1] [,2]  [,3]  [,4]  [,5]  [,6]
[1,] FALSE TRUE FALSE  TRUE  TRUE FALSE
[2,]  TRUE TRUE  TRUE  TRUE  TRUE  TRUE
[3,] FALSE TRUE FALSE FALSE FALSE  TRUE
[4,] FALSE TRUE  TRUE FALSE FALSE FALSE
[5,]  TRUE TRUE  TRUE  TRUE FALSE FALSE
[6,]  TRUE TRUE  TRUE  TRUE FALSE FALSE

I really would like just one comprehensive value to say TRUE or FALSE.

This is the hack (rather ugly I think) I put together that works,
but there has to be a nicer way, no?

res=pop[1:ROWS,] == keep[1:ROWS,]

if ((ROWS*COL) == sum(res))
 {
   cat('they are equal\n')
 }else
   cat('they are NOT equal\n')

Thanks!

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] comparing matrices using max or min

2008-11-06 Thread Dimitris Rizopoulos

you just need pmax() or pmin(), e.g., check this:

set.seed(123)
M1 <- matrix(rnorm(20), 4, 5)
M2 <- matrix(rnorm(20), 4, 5)
M3 <- matrix(rnorm(20), 4, 5)

M1; M2; M3
pmax(M1, M2, M3)
pmin(M1, M2, M3)


I hope it helps.

Best,
Dimitris


Diogo André Alagador wrote:

Dear all,
 
I have 3 matrices with the same dimension, A,B,C and I would like to produce

a matrix D where in each position would retrieve the max(or min) value along
A,B,C taken from the same position.
I guess that apply functions should fit, but for matrices objects I am not
getting it.
 
thanks in advance,
 
Diogo André Alagador


[[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.


--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

__
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] comparing matrices using max or min

2008-11-06 Thread stephen sefick
#how about this
A <- rnorm(10)
B <- rnorm(10)
C <- rnorm(10)

D <- data.frame(A,B,C)

apply(D, MARGIN=1, FUN=min)

On Thu, Nov 6, 2008 at 10:00 AM, Diogo André Alagador
<[EMAIL PROTECTED]> wrote:
> Dear all,
>
> I have 3 matrices with the same dimension, A,B,C and I would like to produce
> a matrix D where in each position would retrieve the max(or min) value along
> A,B,C taken from the same position.
> I guess that apply functions should fit, but for matrices objects I am not
> getting it.
>
> thanks in advance,
>
> Diogo André Alagador
>
>[[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.
>
>



-- 
Stephen Sefick
Research Scientist
Southeastern Natural Sciences Academy

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

-K. Mullis
__
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.


[R] comparing matrices using max or min

2008-11-06 Thread Diogo André Alagador
Dear all,
 
I have 3 matrices with the same dimension, A,B,C and I would like to produce
a matrix D where in each position would retrieve the max(or min) value along
A,B,C taken from the same position.
I guess that apply functions should fit, but for matrices objects I am not
getting it.
 
thanks in advance,
 
Diogo André Alagador

[[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.