changeset 28520:627da618dcc4 stable

hilb.m: Speed-up function using broadcasting. * scripts/special-matrix/hilb.m: Speed-up function using broadcasting. For more information read https://octave.discourse.group/t/boosting-hilb/48 .
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 01 Jul 2020 16:03:41 +0900
parents 49f4d7814760
children 23a33db2bdb3 286fe9352cd6
files scripts/special-matrix/hilb.m
diffstat 1 files changed, 4 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/special-matrix/hilb.m	Mon Jun 29 15:01:27 2020 -0400
+++ b/scripts/special-matrix/hilb.m	Wed Jul 01 16:03:41 2020 +0900
@@ -66,12 +66,10 @@
     error ("hilb: N must be a scalar integer");
   endif
 
-  retval = zeros (n);
-  tmp = 1:n;
-  for i = 1:n
-    retval(i, :) = 1.0 ./ tmp;
-    tmp += 1;
-  endfor
+  ## Very elegant solution by N. Higham
+  ## https://nhigham.com/2020/06/30/what-is-the-hilbert-matrix/
+  j = 1:n;
+  retval = 1 ./ (j' + j - 1);
 
 endfunction