Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 580d94a

Browse files
committed
fixed corrcoef bug
svn path=/trunk/matplotlib/; revision=607
1 parent c063537 commit 580d94a

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
New entries should be added at the top
22

33
==============================================================
4+
2004-10-20 Fixed corrcoef bug exposed by corrcoef(X) where X is matrix
5+
- JDH
6+
47
2004-10-19 Added kwarg support to xticks and yticks to set ticklabel
58
text properties -- thanks to T. Edward Whalen for the suggestion
69

lib/matplotlib/mlab.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,8 @@ def cohere(x, y, NFFT=256, Fs=2, detrend=detrend_none,
276276

277277
def corrcoef(*args):
278278
"""
279-
280279
corrcoef(X) where X is a matrix returns a matrix of correlation
281-
coefficients for each row of X.
280+
coefficients for each numrows observations and numcols variables.
282281
283282
corrcoef(x,y) where x and y are vectors returns the matrix or
284283
correlation coefficients for x and y.
@@ -287,9 +286,10 @@ def corrcoef(*args):
287286
288287
The correlation matrix is defined from the covariance matrix C as
289288
290-
r(i,j) = C[i,j] / (C[i,i]*C[j,j])
289+
r(i,j) = C[i,j] / sqrt(C[i,i]*C[j,j])
291290
"""
292291

292+
293293
if len(args)==2:
294294
X = transpose(array([args[0]]+[args[1]]))
295295
elif len(args)==1:
@@ -299,8 +299,18 @@ def corrcoef(*args):
299299

300300

301301
C = cov(X)
302-
d = resize(diagonal(C), (2,1))
303-
denom = sqrt(matrixmultiply(d,transpose(d)))
302+
303+
if len(args)==2:
304+
d = resize(diagonal(C), (2,1))
305+
denom = sqrt(matrixmultiply(d,transpose(d)))
306+
else:
307+
dc = diagonal(C)
308+
N = len(dc)
309+
shape = N,N
310+
vi = resize(dc, shape)
311+
denom = sqrt(vi*transpose(vi)) # element wise multiplication
312+
313+
304314
r = divide(C,denom)
305315
try: return r.real
306316
except AttributeError: return r
@@ -1140,7 +1150,7 @@ def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0,
11401150
Most of this module requires Numerical Python or is meant to be used with it.
11411151
See http://www.pfdubois.com/numpy for details.
11421152
1143-
Copyright (c) 2001-2004, Fernando Pérez. <[email protected]>
1153+
Copyright (c) 2001-2004, Fernando Perez. <[email protected]>
11441154
All rights reserved.
11451155
11461156
This license was generated from the BSD license template as found in:

0 commit comments

Comments
 (0)