|
1 | 1 | function spC = spConv(spA,spB,shape) |
2 | 2 | %SPCONV N-dimensional convolution of two N-dimensional sparse array structures. |
| 3 | +% |
3 | 4 | % spC = spConv(spA,spB,shape): N-dimensional convolution of two N-dimensional |
4 | 5 | % full arrays, each represented as a sparse array structure or a full array. |
5 | 6 | % The output is a sparse array structure. |
|
45 | 46 | indASzC = spSub2spInd(sizC,subsA); |
46 | 47 | indBSzC = spSub2spInd(sizC,subsB); |
47 | 48 |
|
48 | | -% Do the convolution |
| 49 | +%% Do the convolution |
49 | 50 | indC = indASzC + indBSzC' - 1; % Outer sum of indices, minus 1 |
50 | 51 | indC = indC(:); |
51 | 52 | valC = spA.Val.*spB.Val'; % Outer product of values |
52 | 53 | valC = valC(:); |
53 | 54 | sparseC = sparse(indC,1,valC); % Accumulate (sum) over repeated indices |
54 | 55 |
|
55 | | -% Make sparse array structure for full convolution |
| 56 | +%% Make sparse array structure for full convolution |
56 | 57 | [indC,~,valC] = find(sparseC); |
57 | | -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 58 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
58 | 59 | spC = struct('Size',sizC,'Ind',indC,'Val',valC); |
59 | | -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
60 | 61 |
|
61 | 62 | if isequal(shape,'same') % Remove entries outside size A |
62 | 63 | % Convert linear indices of spC to subs |
|
69 | 70 | % convert subsCTrunc to linear index for array of size spA.Size |
70 | 71 | indC = spSub2spInd(spA.Size,subsCTrunc); |
71 | 72 | % Make into a sparse array structure |
72 | | -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 73 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
73 | 74 | spC = struct('Size',spA.Size,'Ind',indC,'Val',valC); |
74 | | -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
75 | 76 | elseif isequal(shape,'circ') % Wrap entries around size A |
76 | 77 | % Convert linear indices of spC to subs |
77 | 78 | subsC = spInd2spSub(spC); |
|
83 | 84 | sparseC = sparse(indC,1,valC); |
84 | 85 | % Make into a sparse array structure |
85 | 86 | [indC,~,valC] = find(sparseC); |
86 | | -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 87 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
87 | 88 | spC = struct('Size',spA.Size,'Ind',indC,'Val',valC); |
88 | | -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 89 | + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
89 | 90 | end |
90 | 91 |
|
91 | 92 | end |
0 commit comments