Mercurial > hg > octave-nkf
diff scripts/special-matrix/toeplitz.m @ 9126:5780b3b80425
optimize toeplitz for sparse args
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 16 Apr 2009 11:20:49 +0200 |
parents | 38c3a0f8c6d0 |
children | f0c3d3fc4903 |
line wrap: on
line diff
--- a/scripts/special-matrix/toeplitz.m +++ b/scripts/special-matrix/toeplitz.m @@ -91,15 +91,28 @@ c(1) = conj (c(1)); endif - ## Concatenate data into a single column vector. - data = [r(end:-1:2)(:); c(:)]; + if (issparse(c) && issparse(r)) + c = c(:).'; + r = r(:).'; + cidx = find(c); + ridx = find(r); + + ## Ignore the first element in r. + ridx = ridx(ridx > 1); - ## Get slices. - slices = cellslices (data, nc:-1:1, nc+nr-1:-1:nr); + ## Form matrix. + retval = spdiags(repmat(c(cidx),nr,1),1-cidx,nr,nc)+... + spdiags(repmat(r(ridx),nr,1),ridx-1,nr,nc); + else + ## Concatenate data into a single column vector. + data = [r(end:-1:2)(:); c(:)]; - ## Form matrix. - retval = horzcat (slices{:}); + ## Get slices. + slices = cellslices (data, nc:-1:1, nc+nr-1:-1:nr); + ## Form matrix. + retval = horzcat (slices{:}); + endif endfunction %!assert((toeplitz (1) == 1