Compute correlation of two NaN-containing vectors
Show older comments
Hi all! If I need to find the correlation between two NaN-containing vectors by using corrcoef() or corr(), is it possible? How should I set up the parameters? and if not how is it possible to find the correlation between them? Thank you!
Here are two vectors:


Answers (1)
the cyclist
on 27 Mar 2017
Edited: the cyclist
on 27 Mar 2017
The documentation for corrcoef has a section explicitly discussing different methods to handle NaN values. Click here to jump directly to that section.
Since you only have two vectors,
R = corrcoef(A,'rows','pairwise')
and
R = corrcoef(A,'rows','complete')
should give you identical results. (They could be different if you had more than 2 columns, depending on the pattern of NaN values.)
3 Comments
Zhonghao Liao
on 27 Mar 2017
the cyclist
on 27 Mar 2017
Edited: the cyclist
on 27 Mar 2017
The inputs are column vectors. But you are specifying that the rows of each column need to be complete. For example,
x = rand(5,1);
y = rand(5,1);
x(1) = nan;
y(2) = nan;
R = corrcoef(x,y,'rows','complete')
Zhonghao Liao
on 27 Mar 2017
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!