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
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)), f
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
3 matches
Mail list logo