Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-26 Thread Ravi Varadhan
@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jonathan Sent: Monday, April 26, 2010 2:53 PM To: Dennis Murphy Cc: r-help Subject: Re: [R] reordering of matrix rows to maximize the sum of the diagonal Hi Dennis, Thanks for the idea, but the order of the rowSums does not nece

Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-26 Thread Jonathan
Hi Dennis, Thanks for the idea, but the order of the rowSums does not necessarily correspond to the order of rows that maximizes the "rank" of the matrix. Ex: > a[1:9]<-c(1,1,30,50,1,1,1,20,1) > a [,1] [,2] [,3] [1,]1 501 [2,]11 20 [3,] 3011 > a[order(rowSum

Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-23 Thread Dennis Murphy
Hi: How about this? Calling your matrix a, a[order(rowSums(a)), ] [,1] [,2] [,3] [1,]912 [2,]2 111 [3,]34 13 HTH, Dennis On Fri, Apr 23, 2010 at 1:10 PM, Jonathan wrote: > Hi r-help community, >This question isn't so much a syntax/coding one, but here

Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-23 Thread Jonathan
Hi Dimitris, This code is very helpful, although I note that the columns are not preserved, ie if > M <- matrix(sample(13, 9, TRUE), 3, 3) produces: > M [,1] [,2] [,3] [1,] 1392 [2,] 101 12 [3,] 1167 then > n <- ncol(M) > ind <- apply(M, 2, which.max) > maxs

Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-23 Thread Jonathan
David, I'm not entirely sure at first glance (guess this is part of the problem!), but after a little searching around, it does look like this can be addressed using the "Hungarian algorithm," which reorders the rows of a (square) matrix to minimize the sum of the elements along the diagonal (the t

Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-23 Thread Dimitris Rizopoulos
one way is: M <- matrix(sample(13, 9, TRUE), 3, 3) n <- ncol(M) ind <- apply(M, 2, which.max) maxs <- M[cbind(ind, 1:n)] diags <- diag(M) diag(M) <- M[cbind(ind, 1:n)] M[cbind(ind, 1:n)] <- diags M I hope it helps. Best, Dimitris On 4/23/2010 10:10 PM, Jonathan wrote: Hi r-help community,

Re: [R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-23 Thread David Winsemius
Can you specify how you would re-order this matrix: matrix(1:16, 4,4) > apply(mtx, 2, which.max) [1] 4 4 4 4 -- David. On Apr 23, 2010, at 4:10 PM, Jonathan wrote: Hi r-help community, This question isn't so much a syntax/coding one, but here goes: Let's say I have matrix of arbitrary dim

[R] reordering of matrix rows to maximize the sum of the diagonal

2010-04-23 Thread Jonathan
Hi r-help community, This question isn't so much a syntax/coding one, but here goes: Let's say I have matrix of arbitrary dimensions and I'd like to reorder the rows in such a way that I could maximize the sum of the entries along the diagonal. For example, for this 3x3 matrix: [,1] [,