Suggestions for optimizing my code

Robert Kern rkern at ucsd.edu
Fri Aug 5 20:30:16 CEST 2005


drife wrote:
> Hello,
> 
> I am looking for suggestions for how I might optimize my
> code (make it execute faster), and make it more streamlined/
> elegent. But before describing my code, I want to state that
> I am not a computer scientist (I am an atmospheric scientist),
> and have but a rudimentary understanding of OO principles.
> 
> My problem is this: I want to find the maximum off-diagonal
> element of a correlation matrix, and the -position- of that
> element within the matrix. In case you are unfamiliar with
> correlation matrices they are square and symmetric, and contain
> the mutual correlations among data collected at different
> points in space or time.

import MLab
import Numeric as N

def find_max(R):
     U = MLab.triu(R)
     n = U.shape[0]
     # set the diagonal elements to 0.0, too
     U.flat[::n+1] = 0.0
     k = N.argmax(U.flat)
     i, j = divmod(k, n)
     return i, j, R[i,j]

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list