|
| 1 | +function spC = spTrunc(lo,hi,spA) |
| 2 | +%SPTRUNC Truncate a sparse array structure. |
| 3 | +% spC = spTrunc(limits,spA): Truncate a sparse array, represented as a sparse |
| 4 | +% array structure or a full array, so all entries with subscripts lower than |
| 5 | +% the corresponding entries in the row vector 'lo', and higher than the |
| 6 | +% corresponding entries in the row vector 'hi' are removed. The output is |
| 7 | +% sparse array structure with size hi-lo+1. |
| 8 | +% |
| 9 | +% Version 1.0 by Andrew J. Milne, The MARCS Institute, Western Sydney |
| 10 | +% University, 2018-01-09 |
| 11 | + |
| 12 | +if nargin ~= 3 |
| 13 | + error('Three arguments are required.') |
| 14 | +end |
| 15 | +if size(lo,1)>1 || size(hi,1)>1 |
| 16 | + error('''lo'' and ''hi'' must be row vectors.') |
| 17 | +end |
| 18 | +% If full array, make into sparse array structure |
| 19 | +if ~isstruct(spA) |
| 20 | + spA = array2spArray(spA); |
| 21 | +end |
| 22 | +if numel(lo) ~= numel(spA.Size) || numel(hi) ~= numel(spA.Size) |
| 23 | + error('''lo'' and ''hi'' must have the same number of entries as dimensions in the sparse array structure.') |
| 24 | +end |
| 25 | + |
| 26 | +subsA = spInd2spSub(spA); |
| 27 | + |
| 28 | +dimC = hi-lo+1; |
| 29 | +valC = spA.Val; |
| 30 | +subsC = subsA; |
| 31 | + |
| 32 | +valC = valC(all(subsC>=lo & subsC<=hi,2),:); |
| 33 | +subsC = subsC(all(subsC>=lo & subsC<=hi,2),:); |
| 34 | +subsC = subsC - lo + 1; |
| 35 | + |
| 36 | +% convert subsC to linear index for array of size dimC |
| 37 | +if isempty(subsC) |
| 38 | + warning('All array entries have been truncated.') |
| 39 | + indC = []; |
| 40 | + valC = []; |
| 41 | +else |
| 42 | + indC = spSub2spInd(dimC,subsC); |
| 43 | +end |
| 44 | +% Make the sparse array structure |
| 45 | +spC = struct('Size',dimC,'Ind',indC,'Val',valC); |
0 commit comments