-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspSum.m
31 lines (25 loc) · 885 Bytes
/
spSum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function spC = spSum(spA,dim)
%SPSUM Sum sparse array structure over dimension 'dim'.
%
% spC = spSum(spA,dim): Sum full array, represented as a sparse array
% structure or a full array, over the dimension specified as a scalar in
% 'dim'. The output is a sparse array structure.
%
% By Andrew J. Milne, The MARCS Institute, Western Sydney University
%
% See also SPIND2SPSUB, SPSUB2SPIND, SPARSE, SUM.
% If full array, convert to sparse array structure
if ~isstruct(spA)
spA = array2SpArray(spA);
end
sumDim = spA.Size;
if dim <= numel(spA.Size)
sumDim(dim) = [];
end
subA = spInd2SpSub(spA); % get subscripts
subA(:,dim) = 1; % collapse over dim
indA = spSub2SpInd(spA.Size,subA); % convert to linear index
sumSpTerm = sparse(indA,1,spA.Val); % sum over repeated indices
[indA,~,valA] = find(sumSpTerm);
spC = struct('Size',sumDim,'Ind',indA,'Val',valA);
end