Dear R users, i want to check matrices if they are identical when i change the rows or the columns or the signs of the one or more columns
isomorphic <- function (m1, m2) { combs.c <- combn(ncol(m1), 2) nc <- ncol(combs.c) ind.c <- vector("logical", nc) for (i in 1:nc) { m <- m1 m[, combs.c[, i]] <- m[, rev(combs.c[, i])] ind.c[i] <- all(m == m2) } combs.r <- combn(nrow(m1), 2) nr <- ncol(combs.r) ind.r <- vector("logical", nr) for (i in 1:nr) { m <- m1 m[combs.r[, i], ] <- m[rev(combs.r[, i]), ] ind.r[i] <- all(m == m2) } ind.sgn <- lapply(1:ncol(m1), combn, x = ncol(m1)) ind.sgn <- unlist(lapply(ind.sgn, function (ind) { ncc <- ncol(ind) out <- vector("logical", ncc) for (l in 1:ncc) { m <- m1 m[, ind[, l]] <- -m[, ind[, l]] out[l] <- all(m == m2) } out })) any(ind.c, ind.r, ind.sgn) } example matrices a<-matrix(c(1,1,1,1,-1,-1,-1,-1,-1,1,1,1),ncol=4,byrow=T) a1<-matrix(c(-1,-1,-1,-1,1,1,1,1,-1,1,1,1),ncol=4,byrow=T) a2<-matrix(c(1,1,1,1,-1,-1,-1,-1,1,1,1,1),ncol=4,byrow=T) a3<-matrix(c(1,1,1,1,-1,-1,-1,-1,1,1,1,-1),ncol=4,byrow=T) a4<-matrix(c(1,1,1,-1,-1,-1,-1,1,-1,1,1,-1),ncol=4,byrow=T) a5<-matrix(c(1,1,1,-1,-1,-1,-1,1,1,-1,1,-1),ncol=4,byrow=T) a6<-matrix(c(1,1,1,1,-1,-1,-1,-1,1,-1,1,1),ncol=4,byrow=T) the programme works perfeclty when it checks the rows and the columns seperatly and when it check the signs seperatly but it doesnt work correcly when i have the combination of the rows, columns, signs ps in the above patrices only the a2 is Truly False. all the are must be true and the problem i have is a5 because i change signs in last columns and the order of the first 2 rows [[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.