Re: find the maximum sum of a subset of a matrix

John Wallace (jrw@fish.washington.edu)
Wed, 28 Jan 1998 16:54:30 -0500


order() is not needed here, just a reverse sort:

> x_matrix(runif(16), 4, 4)

> x
[,1] [,2] [,3] [,4]
[1,] 0.87142231 0.34707002 0.5597021 0.05678552
[2,] 0.65830855 0.09626004 0.2516509 0.59265213
[3,] 0.02056983 0.32927421 0.3797600 0.59392467
[4,] 0.09846412 0.65736637 0.2434309 0.53036410

> rev(sort(x))[1:5]
[1] 0.8714223 0.6583086 0.6573664 0.5939247 0.5926521

> sum(rev(sort(x))[1:5])
[1] 3.373674

-- 
John Wallace
University of Washington                    ^    ^    ^
Fisheries Research Institute               / \  / \  / \   ^
Box 357980                                 / \  / \   |   / \
Seattle, WA 98195-7980                      |    |  o__~  / \
PHONE   (206) 543-1513                  @ @         /\/\   |
FAX     (206) 685-7471                   ~    
E-MAIL        jw@u.washington.edu              o
WWW      http://www.fish.washington.edu/people/jrw/Wallace.html 
                                                o  _///_ //
                                                <`)=  _<<
                                                    \\\  \\