Skip to content

Trouble sorting the matrix

3 messages · Ola Cabaj, andrija djurovic, 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)), 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: