diff scripts/statistics/corr.m @ 31918:e67b7b85670b

cov.m: Overhaul function for matlab compatibility (bug #50583). * cov.m: Change two-array input handling such that separate x and y inputs are treated as two univariate distributions equivalent to cov (x(:), y(:)). Implement NANFLAG option for selectively ignoring NaN data elements. Correct empty input handling to produce Matlab compatible output. Add BISTs for new features and expand input validation tests. Update docstring to address new behaviors and at note about backwards incompatibility. * corr.m, corrcoef.m: Adapt calls to cov to account for the increase in cov return size. * etc/NEWS.9.md: Add note of changes to Matlab Compatibility section.
author Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
date Wed, 22 Mar 2023 13:33:46 -0400
parents 597f3ee61a48
children 37e184a83cf4
line wrap: on
line diff
--- a/scripts/statistics/corr.m	Wed Mar 22 12:00:05 2023 -0700
+++ b/scripts/statistics/corr.m	Wed Mar 22 13:33:46 2023 -0400
@@ -70,7 +70,20 @@
   ## No check for division by zero error, which happens only when
   ## there is a constant vector and should be rare.
   if (nargin == 2)
-    c = cov (x, y);
+    ## Adjust for Octave 9.1.0 compatability behavior change in two-input cov.
+    ## cov now treats cov(x,y) as cov(x(:),y(:)), returning a 2x2 covariance
+    ## of the two univariate distributions x and y.  corr will now pass [x,y]
+    ## as cov([x,y]), which for m x n inputs will return 2n x 2n outputs, with
+    ##  the off diagonal matrix quarters containing what was previously
+    ## returned by cov(x,y).
+
+    ## FIXME: Returning a larger than needed arary and discarding 3/4 of the
+    ##        information is nonideal.  Consider implementing a more
+    ##        efficient cov here as a subfunction to corr.
+
+    nx = columns(x);
+    c = cov ([x, y]);
+    c = c(1:nx, nx+1:end);
     s = std (x)' * std (y);
     r = c ./ s;
   else