Jonathan
2010-Apr-23 20:10 UTC
[R] reordering of matrix rows to maximize the sum of the diagonal
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] [,2] [,3] [1,] 3 4 13 [2,] 9 1 2 [3,] 2 11 1 rearranging the rows to maximize the sum along the diagonal would produce this matrix: [,1] [,2] [,3] [1,] 9 1 2 [2,] 2 11 1 [3,] 3 4 13 I've been experimenting with some scripts of my own, but I figured I'd ask if one of you R-ninjas might know of an existing function (or algorithm I could look up and then code) that can do this somewhat efficiently (or even just correctly!). Best, Jonathan
David Winsemius
2010-Apr-23 20:20 UTC
[R] reordering of matrix rows to maximize the sum of the diagonal
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 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] [,2] [,3] > [1,] 3 4 13 > [2,] 9 1 2 > [3,] 2 11 1 > > rearranging the rows to maximize the sum along the diagonal would > produce this matrix: > > [,1] [,2] [,3] > [1,] 9 1 2 > [2,] 2 11 1 > [3,] 3 4 13 > > > I've been experimenting with some scripts of my own, but I figured I'd > ask if one of you R-ninjas might know of an existing function (or > algorithm I could look up and then code) that can do this somewhat > efficiently (or even just correctly!). > > Best, > Jonathan > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Dimitris Rizopoulos
2010-Apr-23 20:32 UTC
[R] reordering of matrix rows to maximize the sum of the diagonal
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, > 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] [,2] [,3] > [1,] 3 4 13 > [2,] 9 1 2 > [3,] 2 11 1 > > rearranging the rows to maximize the sum along the diagonal would > produce this matrix: > > [,1] [,2] [,3] > [1,] 9 1 2 > [2,] 2 11 1 > [3,] 3 4 13 > > > I've been experimenting with some scripts of my own, but I figured I'd > ask if one of you R-ninjas might know of an existing function (or > algorithm I could look up and then code) that can do this somewhat > efficiently (or even just correctly!). > > Best, > Jonathan > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Dennis Murphy
2010-Apr-24 05:24 UTC
[R] reordering of matrix rows to maximize the sum of the diagonal
Hi: How about this? Calling your matrix a, a[order(rowSums(a)), ] [,1] [,2] [,3] [1,] 9 1 2 [2,] 2 11 1 [3,] 3 4 13 HTH, Dennis On Fri, Apr 23, 2010 at 1:10 PM, Jonathan <jonsleepy@gmail.com> 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 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] [,2] [,3] > [1,] 3 4 13 > [2,] 9 1 2 > [3,] 2 11 1 > > rearranging the rows to maximize the sum along the diagonal would > produce this matrix: > > [,1] [,2] [,3] > [1,] 9 1 2 > [2,] 2 11 1 > [3,] 3 4 13 > > > I've been experimenting with some scripts of my own, but I figured I'd > ask if one of you R-ninjas might know of an existing function (or > algorithm I could look up and then code) that can do this somewhat > efficiently (or even just correctly!). > > Best, > Jonathan > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]