I would like to sort matrix3, so that every column in output matrix is sorted. Also I have to "unsort" it later on. matrix1<-matrix(rnorm(100,354,78),ncol=10) matrix2<-matrix(rnorm(100,225,102),ncol=10) matrix3<-cbind(matrix1,matrix2) nrCol<-length(matrix3[1,]) class1<-1:10 for(i in 1:nrCol) { sorted.matrix<-matrix3[order(matrix3[class1,i]),] } for(i in 1:liczbaKolumn) { output<-sorted.matrix[order(matrix3[class1,i]),] } -- Aleksandra Cabaj [[alternative HTML version deleted]]
Hi. Here is an example of sorting matrix columns:> mat <- matrix(10:1, ncol=2, byrow=TRUE) > mat[,1] [,2] [1,] 10 9 [2,] 8 7 [3,] 6 5 [4,] 4 3 [5,] 2 1> apply(mat, 2, function(x) x[order(x)])[,1] [,2] [1,] 2 1 [2,] 4 3 [3,] 6 5 [4,] 8 7 [5,] 10 9 You should read help page [, apply and order (?"[", ?apply, ?order) Hope this helps Andrija On Sat, Jun 15, 2013 at 12:36 PM, Ola Cabaj <olacabaj@gmail.com> wrote:> I would like to sort matrix3, so that every column in output matrix is > sorted. Also I have to "unsort" it later on. > > matrix1<-matrix(rnorm(100,354,78),ncol=10) > matrix2<-matrix(rnorm(100,225,102),ncol=10) > matrix3<-cbind(matrix1,matrix2) > nrCol<-length(matrix3[1,]) > class1<-1:10 > for(i in 1:nrCol) > { > sorted.matrix<-matrix3[order(matrix3[class1,i]),] > } > for(i in 1:liczbaKolumn) > { > output<-sorted.matrix[order(matrix3[class1,i]),] > } > > -- > Aleksandra Cabaj > > [[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. >[[alternative HTML version deleted]]
Hello, If I understand it correctly, the following should sort all columns of matrix3 and then unsort the sorted matrix. ord <- apply(matrix3, 2, order) # sort matrix3 mat4 <- sapply(seq_len(ncol(ord)), function(i) matrix3[ord[,i], i]) mat4 # unsort mat4 mat5 <- sapply(seq_len(ncol(ord)), function(i) mat4[order(ord[,i]), i]) identical(matrix3, mat5) # TRUE Hope this helps, Rui Barradas Em 15-06-2013 11:36, Ola Cabaj escreveu:> I would like to sort matrix3, so that every column in output matrix is > sorted. Also I have to "unsort" it later on. > > matrix1<-matrix(rnorm(100,354,78),ncol=10) > matrix2<-matrix(rnorm(100,225,102),ncol=10) > matrix3<-cbind(matrix1,matrix2) > nrCol<-length(matrix3[1,]) > class1<-1:10 > for(i in 1:nrCol) > { > sorted.matrix<-matrix3[order(matrix3[class1,i]),] > } > for(i in 1:liczbaKolumn) > { > output<-sorted.matrix[order(matrix3[class1,i]),] > } >
Hello, Yes, you can do it like you say. Or you can unsort first and multiply later. mat5.1 <- sapply(seq_len(ncol(ord)), function(i) mat4[order(ord[,i]), i]) multip<-mat4*2 mat5.2 <- sapply(seq_len(ncol(ord)), function(i) multip[order(ord[,i]), i]) identical(mat5.1*2, mat5.2) #TRUE Also, it's better if you Cc the list, for others to read and eventually find it of some use. Rui Barradas Em 15-06-2013 12:48, Ola Cabaj escreveu:> Thanks, that helps a lot! I got another question, if you don't mind. > If I would like to multiply mat4 by a number/vector > multip<-mat4*2 > > so to unsort this multiplied matrix I have to change mat4 to multip? > mat5 <- sapply(seq_len(ncol(ord)), function(i) multip[order(ord[,i]), i]) > > > 2013/6/15 Rui Barradas <ruipbarradas at sapo.pt <mailto:ruipbarradas at sapo.pt>> > > Hello, > > If I understand it correctly, the following should sort all columns > of matrix3 and then unsort the sorted matrix. > > > ord <- apply(matrix3, 2, order) > > # sort matrix3 > mat4 <- sapply(seq_len(ncol(ord)), function(i) matrix3[ord[,i], i]) > mat4 > > # unsort mat4 > mat5 <- sapply(seq_len(ncol(ord)), function(i) mat4[order(ord[,i]), i]) > > identical(matrix3, mat5) # TRUE > > > Hope this helps, > > Rui Barradas > > Em 15-06-2013 11:36, Ola Cabaj escreveu: > > I would like to sort matrix3, so that every column in output > matrix is > sorted. Also I have to "unsort" it later on. > > matrix1<-matrix(rnorm(100,354,__78),ncol=10) > matrix2<-matrix(rnorm(100,225,__102),ncol=10) > matrix3<-cbind(matrix1,__matrix2) > nrCol<-length(matrix3[1,]) > class1<-1:10 > for(i in 1:nrCol) > { > sorted.matrix<-matrix3[order(__matrix3[class1,i]),] > } > for(i in 1:liczbaKolumn) > { > output<-sorted.matrix[order(__matrix3[class1,i]),] > } > > > > > -- > Aleksandra Cabaj