[R] Trouble sorting the matrix

2013-06-15 Thread Ola Cabaj
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) {

Re: [R] Trouble sorting the matrix

2013-06-15 Thread andrija djurovic
Hi. Here is an example of sorting matrix columns: mat - matrix(10:1, ncol=2, byrow=TRUE) mat [,1] [,2] [1,] 109 [2,]87 [3,]65 [4,]43 [5,]21 apply(mat, 2, function(x) x[order(x)]) [,1] [,2] [1,]21 [2,]43 [3,]65 [4,]8

Re: [R] Trouble sorting the matrix

2013-06-15 Thread Rui Barradas
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)),

Re: [R] Trouble sorting the matrix

2013-06-15 Thread Rui Barradas
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