# HG changeset patch # User Kai T. Ohlhus # Date 1593587021 -32400 # Node ID 627da618dcc4b7e84732c9c132c35ea5738070b3 # Parent 49f4d7814760957f2fc5ca5514929eea577a95af 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 . diff -r 49f4d7814760 -r 627da618dcc4 scripts/special-matrix/hilb.m --- 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