Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: using corr2 with binary vectors

Subject: using corr2 with binary vectors

From: james_1

Date: 6 Feb, 2008 17:14:00

Message: 1 of 4

Hi All,

I am trying to compare two binary vectors. Sometimes the corr2 function returns a number, and other times it returns NaN.

For example, I do not understand why these commands do not output numbers...

x = [0 0 0 0 0 0 1 0];
y = [0 0 0 0 0 0 1 0];
corr2(x,y) % should = 1 because they are identical

x = [0 0 0 0 0 0 0 0];
y = [0 0 0 0 0 0 0 0];
corr2(x,y) % should = 1 because they are identical

x = [0 0 0 0 1 0 1 0];
y = [0 0 0 0 0 0 0 0];
corr2(x,y) % should != 1 because they are different


Instead of seeing an output of "1" "1" "0.7", I see
"1" "NaN" "NaN".

Does anyone know how I can successfully compare variables in this format?

Many thanks

Subject: using corr2 with binary vectors

From: Aaron Callard

Date: 6 Feb, 2008 17:42:02

Message: 2 of 4


If you examine the formula in the docmentation you see
that it divides the correlation by the magnitude of the
vectors so, anything which has a zero magnitude will have
a NAN.

Are you just trying to calcuale the number of like
elements as a funciton of the whole

why don't you just do it directly rather than use a
function

i.e.
x = logical([0 0 0 0 0 0 0 0]);
y = logical([0 0 0 0 0 0 1 0]);

mean(x==y)

Also where are you getting 0.7 from for the third example,
they are similar for 6 out of 8 elements.



james_1 <arandall85@gmail.com> wrote in message
<29045768.1202318071035.JavaMail.jakarta@nitrogen.mathforum
.org>...
> Hi All,
>
> I am trying to compare two binary vectors. Sometimes
the corr2 function returns a number, and other times it
returns NaN.
>
> For example, I do not understand why these commands do
not output numbers...
>
> x = [0 0 0 0 0 0 1 0];
> y = [0 0 0 0 0 0 1 0];
> corr2(x,y) % should = 1 because they are identical
>
> x = [0 0 0 0 0 0 0 0];
> y = [0 0 0 0 0 0 0 0];
> corr2(x,y) % should = 1 because they are identical
>
> x = [0 0 0 0 1 0 1 0];
> y = [0 0 0 0 0 0 0 0];
> corr2(x,y) % should != 1 because they are different
>
>
> Instead of seeing an output of "1" "1" "0.7", I see
> "1" "NaN" "NaN".
>
> Does anyone know how I can successfully compare
variables in this format?
>
> Many thanks

Subject: using corr2 with binary vectors

From: Steven Lord

Date: 6 Feb, 2008 17:43:33

Message: 3 of 4


"james_1" <arandall85@gmail.com> wrote in message
news:29045768.1202318071035.JavaMail.jakarta@nitrogen.mathforum.org...
> Hi All,
>
> I am trying to compare two binary vectors. Sometimes the corr2 function
> returns a number, and other times it returns NaN.
>
> For example, I do not understand why these commands do not output
> numbers...
>
> x = [0 0 0 0 0 0 1 0];
> y = [0 0 0 0 0 0 1 0];
> corr2(x,y) % should = 1 because they are identical
>
> x = [0 0 0 0 0 0 0 0];
> y = [0 0 0 0 0 0 0 0];
> corr2(x,y) % should = 1 because they are identical

Look at the formula given in the reference page for CORR2:

http://www.mathworks.com/access/helpdesk/help/toolbox/images/index.html?/access/helpdesk/help/toolbox/images/corr2.html&http://www.mathworks.com/cgi-bin/texis/webinator/search/

All the elements of (x-mean(x)) are 0 (since x is a constant vector.) This
means the numerator will be 0 (since each of the terms being summed is 0)
and the denominator will be 0 (since one of the sums under the square root
returns 0.) Thus the correlation coefficient is 0/0 which correctly returns
NaN.

> x = [0 0 0 0 1 0 1 0];
> y = [0 0 0 0 0 0 0 0];
> corr2(x,y) % should != 1 because they are different

This returns NaN for the same reason as above, except that the elements of
(y-mean(y)) are all 0 instead of the elements of (x-mean(x)).

> Instead of seeing an output of "1" "1" "0.7", I see
> "1" "NaN" "NaN".
>
> Does anyone know how I can successfully compare variables in this format?

If you're looking for what percentage of the elements of x agree with the
corresponding element of y, that's easy to compute.

sum(x==y)/numel(x)

For the first and second examples, this returns 1 as you indicated you
expect. For the third example, this returns 0.75, which is fairly close to
the 0.7 you indicated you expected to see as output.

--
Steve Lord
slord@mathworks.com


Subject: using corr2 with binary vectors

From: james_1

Date: 15 Feb, 2008 13:59:54

Message: 4 of 4

Many thanks for your responses guys :)

I understand the division by zero error, and so have gone for using "sum(x==y)/numel(x)" which works a treat!

I may also consider appending a '1' to the end of each array before doing the correlation to remove this error, but for the time being the suggestion formula works great.

Regards,

James

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics