Pearsons correlation using corrcoef not working

조회 수: 4 (최근 30일)
Elizabeth Lees
Elizabeth Lees 2021년 3월 24일
댓글: William Rose 2021년 3월 24일
I am trying to calculate the pearsons correlation between two variables in a timetable. I am utalising the corrcoef() function. However the output it gives me is always NaN NaN NaN NaN. Why is this? How can I fix this?
  댓글 수: 4
William Rose
William Rose 2021년 3월 24일
Are the variables (vectors) which you are trying to correlate both numbers (not strings or times or dates), with the same length?
If you provide the timetable, and the code that does not work, as attached files, it will help others help you. An example of corrcoeff() operating on two columns from a table is below.
>> height=1.7+0.2*randn(10,1);
>> weight=70+10*randn(10,1);
>> T=table(height,weight);
>> corrcoef(T.height,T.weight)
ans =
1.0000 0.2262
0.2262 1.0000
Elizabeth Lees
Elizabeth Lees 2021년 3월 24일
@William Rose Thank you for your answer. I have discovered it was because many rows of the numerical data contained NaN values, I have now used 'Rows', 'Pairwise' to discard these in the statistics and am now recieving results. However when just the NSE() function to calculate the Nash-Sutcliffe value between the variables I am still getting an NaN result I believe for the same problem however 'Rows', 'Pairwise' doesn't seem to work within the NSE() function, do you know of another way to discard these empty rows to calculate the Nash-Sutcliffe?
Example code:
NSE(combined_data.Observed_Rio_Branco,combined_data.Simulated_Rio_Branco, 'Rows', 'pairwise')
Error using NSE
Too many input arguments.

댓글을 달려면 로그인하십시오.

채택된 답변

William Rose
William Rose 2021년 3월 24일
This code eliminates each row with one or more NaNs. Thank you to @Jan for this.
X = rand(10, 4);
X(X < 0.1) = NaN;
disp(X); %array containing NaNs
X(any(isnan(X), 2), :) = []; %delete rows with NaN
disp(X) %array with NaN rows removed
  댓글 수: 1
William Rose
William Rose 2021년 3월 24일
Assumng your input data are column vectors, you would do
%make a Nx2 array
A=[combined_data.Observed_Rio_Branco,combined_data.Simulated_Rio_Branco];
A(any(isnan(A), 2), :) = []; %delete rows with NaN
NSE(A(:,1),A(:,2));
I do not have the NSE() function so I cannot test the code above.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by