<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/247934</link>
    <title>MATLAB Central Newsreader - Sample Correlation Distance Implementation</title>
    <description>Feed for thread: Sample Correlation Distance Implementation</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.co.kr/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 31 Mar 2009 12:53:01 +0000</pubDate>
      <title>Sample Correlation Distance Implementation</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/247934#638957</link>
      <author>Ahmad Ammari</author>
      <description>I am trying to implement a function that computes the sample correlation distance between column vectors given as a matrix (pos). The function is supposed to return a distance matrix that shows the distances between every column vector pair in the matrix. I got the equation of the sample correlation distance from the statistics toolbox. You can take a look at it in the definition of the pdist function in the Matlab help. &lt;br&gt;
&lt;br&gt;
The code of my function is as follows:&lt;br&gt;
&lt;br&gt;
function d = corrdist(pos)&lt;br&gt;
[rows,cols] = size(pos);&lt;br&gt;
d = zeros(cols,cols);&lt;br&gt;
for i=1:cols&lt;br&gt;
&amp;nbsp;&amp;nbsp;for j=1:(i-1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d(i,j) = calc_distance(pos(:,i),pos(:,j));&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
d = d + d';&lt;br&gt;
function d = calc_distance(v1,v2)&lt;br&gt;
v1bar = mean(v1);&lt;br&gt;
v2bar = mean(v2);&lt;br&gt;
v1diff = (v1 - v1bar);&lt;br&gt;
v2diff = (v2 - v2bar);&lt;br&gt;
v1difftr = v1diff';&lt;br&gt;
v2difftr = v2diff';&lt;br&gt;
d = 1 - ((v1diff*v2difftr)/(sqrt(v1diff*v1difftr)*sqrt(v2diff*v2difftr)));&lt;br&gt;
&lt;br&gt;
My problem is that I get the following error when trying to test the function:&lt;br&gt;
&lt;br&gt;
Warning: Matrix is singular to working precision. &lt;br&gt;
&amp;gt; In corrdist&amp;gt;calc_distance at 51&lt;br&gt;
&amp;nbsp;&amp;nbsp;In corrdist at 34&lt;br&gt;
??? Subscripted assignment dimension mismatch.&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; corrdist at 34&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d(i,j) = calc_distance(pos(:,i),pos(:,j));&lt;br&gt;
&lt;br&gt;
I tried to test the code inside calc_distance(v1,v2) in the Matlab command line and it worked fine. So why is that error and how to solve it? &lt;br&gt;
&lt;br&gt;
Thanks in advance.</description>
    </item>
    <item>
      <pubDate>Thu, 02 Apr 2009 00:36:01 +0000</pubDate>
      <title>Re: Sample Correlation Distance Implementation</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/247934#639528</link>
      <author>Roger Stafford</author>
      <description>"Ahmad Ammari" &amp;lt;ammari@acm.org&amp;gt; wrote in message &amp;lt;gqt3nd$64k$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I am trying to implement a function that computes the sample correlation distance between column vectors given as a matrix (pos). The function is supposed to return a distance matrix that shows the distances between every column vector pair in the matrix. I got the equation of the sample correlation distance from the statistics toolbox. You can take a look at it in the definition of the pdist function in the Matlab help. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The code of my function is as follows:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function d = corrdist(pos)&lt;br&gt;
&amp;gt; [rows,cols] = size(pos);&lt;br&gt;
&amp;gt; d = zeros(cols,cols);&lt;br&gt;
&amp;gt; for i=1:cols&lt;br&gt;
&amp;gt;   for j=1:(i-1)&lt;br&gt;
&amp;gt;     d(i,j) = calc_distance(pos(:,i),pos(:,j));&lt;br&gt;
&amp;gt;   end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; d = d + d';&lt;br&gt;
&amp;gt; function d = calc_distance(v1,v2)&lt;br&gt;
&amp;gt; v1bar = mean(v1);&lt;br&gt;
&amp;gt; v2bar = mean(v2);&lt;br&gt;
&amp;gt; v1diff = (v1 - v1bar);&lt;br&gt;
&amp;gt; v2diff = (v2 - v2bar);&lt;br&gt;
&amp;gt; v1difftr = v1diff';&lt;br&gt;
&amp;gt; v2difftr = v2diff';&lt;br&gt;
&amp;gt; d = 1 - ((v1diff*v2difftr)/(sqrt(v1diff*v1difftr)*sqrt(v2diff*v2difftr)));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My problem is that I get the following error when trying to test the function:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Warning: Matrix is singular to working precision. &lt;br&gt;
&amp;gt; &amp;gt; In corrdist&amp;gt;calc_distance at 51&lt;br&gt;
&amp;gt;   In corrdist at 34&lt;br&gt;
&amp;gt; ??? Subscripted assignment dimension mismatch.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Error in ==&amp;gt; corrdist at 34&lt;br&gt;
&amp;gt;     d(i,j) = calc_distance(pos(:,i),pos(:,j));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I tried to test the code inside calc_distance(v1,v2) in the Matlab command line and it worked fine. So why is that error and how to solve it? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks in advance.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Your 'calc_distance' function appears to be designed to accept two row vector inputs.  With two column vectors for v1 and v2 the quantity "v1diff*v2difftr" would give you a square matrix, not a scalar.  When you attempt to find its square root it is not surprising that this matrix is found to be singular; in fact it is so singular it would have a rank of only one!  You need to have "v2difftr*v1diff" instead if you are going to use column vector inputs.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Why is it you aren't using 'pdist' itself with the 'correlation distance' metric?  It seems to be designed to do just your kind of problem, except that it expects to find distances between the rows of its input matrix, not its columns.  You need only do a transpose to adapt to that.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Wed, 08 Apr 2009 11:22:01 +0000</pubDate>
      <title>Re: Sample Correlation Distance Implementation</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/247934#641308</link>
      <author>Ahmad Ammari</author>
      <description>"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;gt;   Your 'calc_distance' function appears to be designed to accept two row vector inputs.  With two column vectors for v1 and v2 the quantity "v1diff*v2difftr" would give you a square matrix, not a scalar.  When you attempt to find its square root it is not surprising that this matrix is found to be singular; in fact it is so singular it would have a rank of only one!  You need to have "v2difftr*v1diff" instead if you are going to use column vector inputs.&lt;br&gt;
&lt;br&gt;
Thanks Roger. You are right and I solved the problem by using the dot(v1,v2) function to get the dot product of the two vectors instead of directly multiplying them. &lt;br&gt;
&lt;br&gt;
&amp;gt;   Why is it you aren't using 'pdist' itself with the 'correlation distance' metric?  It seems to be designed to do just your kind of problem, except that it expects to find distances between the rows of its input matrix, not its columns.  You need only do a transpose to adapt to that.&lt;br&gt;
&lt;br&gt;
Yes I know I could have used the pdist function with the 'correlation' value for the distance parameter but I just wanted to implement the original equation of the sample correlation function so my function can run even if I do not have the statistics toolbox in Matlab installed. &lt;br&gt;
And I have to consider columns not rows as vectors in my implementation because this is how the functions in the neural networks toolbox in Matlab consider vectors to be. </description>
    </item>
  </channel>
</rss>
