Compute correlation of two NaN-containing vectors

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 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

Thanks. I have one more question: what is the meaning of 'rows'? I mean I want to compute the correlation between these two column vectors. But when I try to input R = corrcoef(A,'columns','complete') it does not work and shows valid parameters are 'alpha' and 'rows'. Thanks!
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')

Sign in to comment.

Asked:

on 27 Mar 2017

Commented:

on 27 Mar 2017

Community Treasure Hunt

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

Start Hunting!