Correlation coefficent for vectors (of unequal lengths).

Hi,
I have two separate vectors (A and B) which I want to caculate '[r,p] = corrcoef(A,B)' for. They are the same length but both contain 'NaN' so I can't use corrcoef. Both vectors follow a time series so I can't remove the nans separately in each vector as the rows of data will then be out of sequence.
Is there a way of removing an entire row in both A and B if one of them contains a nan on a particular row so that they remain in synch with each other?
If not is there another method I can use?
Thanks

 채택된 답변

Daniel Shub
Daniel Shub 2013년 7월 25일
편집: Daniel Shub 2013년 7월 25일
First make some dummy data
A = 1:10;
A(2) = nan;
B = 1:10;
B(4)=nan;
Then combine the arrays into a matrix so it is easy to work with
C = [A; B];
Then only keep columns for which not any of the rows are nan
D = C(:, ~any(isnan(C)))
Finally split the matrix into two new arrays
Aprime = D(1, :);
Bprime = D(2, :);

추가 답변 (1개)

Narges M
Narges M 2013년 7월 25일

1 개 추천

use isnan() function to skip those rows

댓글 수: 5

Narges M, thanks for your reply. Do I need to create a loop with isnan? Can you explain please? :)
this will give you the index of the rows containing only numbers: idx = find(~isnan(A)+ ~isnan(B)==2);
I tried using the index to for both vectors together but I get the error; 'Error using + Matrix dimensions must agree'.
I tried finding idx for A and B separately and that works (without '==2'- that didn't work for me I'm afraid) but now I don't know if the vector rows are still in synch. The vectors now are of unequal lengths
the ~isnan(A) function will give you 1s for numbers and 0s for nan elements. by adding the two together (assuming your A and B matrix are exactly the same length), you will get 2s on those rows containing numbers in both A and B. using find() function you would have that index in the number form.
so for example:
A = [ 1 4 7 NaN 8 NaN 3];
B = [NaN 6 4 5 1 NaN 9];
then
idx = find(~isnan(A)+ ~isnan(B)==2)
will give you:
idx = [2 3 5 7];
which is correct. now you can use
corrcoef(A(idx),B(idx)).
Oh I see that clearly now! Thanks!

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

카테고리

도움말 센터File Exchange에서 Data Preprocessing에 대해 자세히 알아보기

질문:

2013년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by