Thanks for your help. Finally I found the solution to this problem for any matrix without having to know its number of columns I write it down in case someone else needed it.
#Creation of b matrix ncol<-3 b <- matrix(0, 2^ncol, ncol) for (i in 1:ncol) b[, ncol+1-i] <- rep(rep(c(0,1),c(2^(i-1),2^(i-1))),2^(ncol-i)) b<-cbind(b,rowSums(b)) mat <- b[ ,ncol(b):1 ] ( reverse columns making last first, etc) b[do.call("order", split(mat, col(mat))),] -- View this message in context: http://n4.nabble.com/inverse-lexicographical-ordering-tp1561930p1562709.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.