[R] sorting elements of matrix by row

Vumani Dlamini dvumani at hotmail.com
Fri Oct 27 12:10:27 CEST 2006


Dear R users,
I would like to sort elements of a matrix by row and use this ordering to 
also sort another matrix. I am trying to post-order the means of components 
for a mixture model and would also like to do the same for the component 
probabilities. This is what I have tried thus far, but I doubt whether its 
efficient given the number of replications I am using:

mean.data <- matrix(rnorm(20,0,1),10,2)
probs <- runif(10)
prob.data <- cbind(probs,1-probs)
sorted.mean <- sorted.prob <- array(0, dim=dim(mean.data))
for(i in 1:nrow(mean.data)){
    sorted.data[i,] <- mean.data[i,order(mean.data[i,])]
    sorted.prob[i,] <- prob.data[i,order(mean.data[i,])]
}

Thanks,
Vumani



More information about the R-help mailing list