From 346aaaa1a6053f7b8376b8c509e53a3c73ca106d Mon Sep 17 00:00:00 2001 From: David Gleich Date: Sat, 16 May 2009 12:13:36 -0700 Subject: [PATCH] Added number of columns output for full round-tripping --- sparse_to_csr.m | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/sparse_to_csr.m b/sparse_to_csr.m index d5cc6e4..5a746ce 100644 --- a/sparse_to_csr.m +++ b/sparse_to_csr.m @@ -1,4 +1,4 @@ -function [rp ci ai]=sparse_to_csr(A,varargin) +function [rp ci ai ncol]=sparse_to_csr(A,varargin) % SPARSE_TO_CSR Convert a sparse matrix into compressed row storage arrays % % [rp ci ai] = sparse_to_csr(A) returns the row pointer (rp), column index @@ -12,7 +12,7 @@ % A=sparse(6,6); A(1,1)=5; A(1,5)=2; A(2,3)=-1; A(4,1)=1; A(5,6)=1; % [rp ci ai]=sparse_to_csr(A) % -% See also SPARSE_TO_CSC +% See also CSR_TO_SPARSE % David Gleich % Copyright, Stanford University, 2008 @@ -20,12 +20,14 @@ % History % 2008-04-07: Initial version % 2008-04-24: Added triple array input +% 2009-05-01: Added ncol output error(nargchk(1, 4, nargin, 'struct')) retc = nargout>1; reta = nargout>2; if nargin>1 - n = varargin{end}; + n = varargin{3}; + if nargin>4, ncol = varargin{4}; end nzi = A; nzj = varargin{1}; if reta && length(varargin) > 2, nzv = varargin{2}; end nz = length(A); @@ -35,11 +37,15 @@ if reta && length(varargin) < 3, error('gaimc:invalidInput',... 'no value array passed for triplet input, see usage'); end if ~isscalar(n), error('gaimc:invalidInput',... - ['the final input to sparse_to_csr with triple input was not ' ... + ['the 4th input to sparse_to_csr with triple input was not ' ... 'a scalar']); end - + if nargin < 5, ncol = max(nzj); + elif ~isscalar(ncol), error('gaimc:invalidInput',... + ['the 5th input to sparse_to_csr with triple input was not ' ... + 'a scalar']); + end else - n = size(A,1); nz=nnz(A); + n = size(A,1); nz = nnz(A); ncol = size(A,2); retc = nargout>1; reta = nargout>2; if reta, [nzi nzj nzv] = find(A); else [nzi nzj] = find(A); @@ -62,4 +68,4 @@ rp(i+1)=rp(i); end rp(1)=0; -rp=rp+1; \ No newline at end of file +rp=rp+1;