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
采纳的回答
更多回答(1 个)
Narges M
2013-7-25
1 个投票
use isnan() function to skip those rows
5 个评论
Anna
2013-7-25
Narges M
2013-7-25
this will give you the index of the rows containing only numbers: idx = find(~isnan(A)+ ~isnan(B)==2);
Anna
2013-7-25
Narges M
2013-7-25
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)).
Anna
2013-7-25
类别
在 帮助中心 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!