Skip to content
Prev 310548 / 398526 Next

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

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