Why do I get NaN from corrcoef12?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Peter Mills
am 6 Apr. 2017
Beantwortet: Walter Roberson
am 7 Apr. 2017
Why does this give me NaN as the result?
load('Varablesuv.mat') %see attached
rho = corrcoef12(invnormcdf(u),invnormcdf(v));
Here is the code for corrcoef12:
function xy = corrcoef(x,y)
%CORRCOEF Correlation coefficients.
% CORRCOEF(X) is a matrix of correlation coefficients formed
% from array X whose each row is an observation, and each
% column is a variable.
% CORRCOEF(X,Y), where X and Y are column vectors is the same as
% CORRCOEF([X Y]).
%
% If C is the covariance matrix, C = COV(X), then CORRCOEF(X) is
% the matrix whose (i,j)'th element is
%
% C(i,j)/SQRT(C(i,i)*C(j,j)).
%
% See also COV, STD.
% J. Little 5-5-86
% Revised 6-9-88 LS 2-13-95 BJ
% Copyright (c) 1984-98 by The MathWorks, Inc.
% $Revision: 5.5 $ $Date: 1997/11/21 23:23:34 $
switch nargin
case 1
c = cov(x);
case 2
c = cov(x,y);
otherwise
error('Not enough input arguments.');
end
d = diag(c);
xy = c./sqrt(d*d');
xy = xy(1,2);
From debugging this I can see that the problem is because: c = cov(x,y); gives NaN's however I'm not clear why cov is giving NaN's?
2 Kommentare
Walter Roberson
am 6 Apr. 2017
Are you getting invnormcdf from https://github.com/beckel/nilm-eval/blob/master/Matlab/lib/lightspeed/invnormcdf.m ?
Akzeptierte Antwort
Walter Roberson
am 7 Apr. 2017
Your data contains 1.0 values. The invnormcdf formula is x = erfinv(2*p-1)*sqrt(2); which for 1.0 would be erfinv(2*1-1) which is erfinv(1) which is inf. Therefore you would be taking cov() of vectors with infinite values, and that is leading to NaN.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!