[R] changing the signs in rows or columns in matrices and check them if they are identical

arun smartpink111 at yahoo.com
Sun Nov 11 17:48:03 CET 2012


Hi,

I guess this should also work for the last two pairs of matrices:


fun3<-function(x,y){
 res<-all(Reduce(paste,data.frame(x))==Reduce(paste,data.frame(y))|Reduce(paste,data.frame(x))==Reduce(paste,-1*data.frame(y)))
 res}
 fun3(x1,y1)
#[1] TRUE
 fun3(x2,y2)
#[1] FALSE

fun4<-function(x,y){
 res<-all(Reduce(paste,data.frame(t(x)))==Reduce(paste,data.frame(t(y)))|Reduce(paste,data.frame(t(x)))==Reduce(paste,-1*data.frame(t(y))))
 res}
fun4(x1,y1)
#[1] FALSE
 fun4(x2,y2)
#[1] TRUE
A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Haris Rhrlp <haris_r_help at yahoo.com>
Cc: "R-help at r-project.org" <R-help at r-project.org>
Sent: Sunday, November 11, 2012 6:04 AM
Subject: Re: [R] changing the signs in rows or columns in matrices and check them if they are identical

Hello,

Thanks for the data examples. But you should give them in a form that's 
easy for us to copy and paste into an R session. Using ?dput, for 
instance. The examples below are the last two pairs of matrices in your 
post, the ones with different signs. fun1() checks rows and fun2() columns.

x1 <- structure(c(1, -1, 1, -1, 1, 1, 1, -1, -1), .Dim = c(3L, 3L))
y1 <- structure(c(1, -1, -1, -1, 1, -1, 1, -1, 1), .Dim = c(3L, 3L))
x2 <- structure(c(1, -1, 1, -1, 1, 1, 1, -1, -1), .Dim = c(3L, 3L))
y2 <- structure(c(1, -1, 1, 1, -1, -1, 1, -1, -1), .Dim = c(3L, 3L))


fun1 <- function(x, y){
     all(sapply(seq_len(nrow(x)), function(i)
         identical(x[i,], y[i,]) || identical(x[i,], -y[i,])))
}
fun2 <- function(x, y){
     all(sapply(seq_len(nrow(x)), function(i)
         identical(x[,i], y[,i]) || identical(x[,i], -y[,i])))
}


fun1(x1, y1)  # TRUE
fun2(x1, y1)  # FALSE

fun1(x2, y2)  # FALSE
fun2(x2, y2)  # TRUE


Hope this helps,

Rui Barradas
Em 11-11-2012 08:06, Haris Rhrlp escreveu:
> Dear R users,
>
> i have this problem with matrices i want to check between two matrices if they are isomorphic i will give an example for what excactly i want
>    1 -1  1                                 -1  1   1
> -1   1  -1                                1  -1  -1
>    1  1   -1                                1   1  -1
> this two matrices are isomorphic beacause if i change the first 2 columns the matrices are identical
>
>    1   -1  1                             -1    1   -1
>   -1    1 -1                               1   -1   1
>    1    1  -1                               1   -1   1
> this two matrices are isomorphic because if i change the first 2 rows the matrices  are identical
>   1     -1   1                         1    -1   1
> -1      1  -1                        -1    1  -1
>    1      1  -1                         -1  -1  1
> this two matrices are isomorphic because in the third line if i change the signs the two matrices are identical
>    1   -1   1                      1    1    1
> -1     1  -1                     -1   -1  -1
>   1     1   -1                       1  -1  -1
> this two matrices are isomorphic because in the second column if i change the signs the two matrices are identical
>
>
> and all the above together
>
> rA <- apply(BB2[,,1,(j-1)],1,paste,collapse=" ")
>      rB <- apply(BB2[,,i,(j-1)],1,paste,collapse=" ")
>      cA <- apply(BB2[,,1,(j-1)],2,paste,collapse=" ")
>      cB <- apply(BB2[,,i,(j-1)],2,paste,collapse=" ")
>      iso <- all(sort(rA) == sort(rB)) | all(sort(cA) == sort(cB))
>
> this is only for  the change or rows and columns but i want to change the signs too.
>
>     [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at 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 at 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.





More information about the R-help mailing list