|
| 1 | +function spC = spFlip(spA,dim) |
| 2 | +%SPFLIP Flip order of entries in dimensions specified in 'dim'. |
| 3 | +% spC = spFlip(spA,dim): Flip the order of entries of the full array, |
| 4 | +% represented as a sparse array structure or full array, in the dimensions |
| 5 | +% specified in the row vector or scalar 'dim'. The output is a sparse array |
| 6 | +% structure. |
| 7 | +% |
| 8 | +% Version 1.0 by Andrew J. Milne, The MARCS Institute, Western Sydney |
| 9 | +% University, 2018-01-16 |
| 10 | +% |
| 11 | +% See also FLIP. |
| 12 | + |
| 13 | +% If full array, convert to sparse array structure |
| 14 | +if nargin < 2 |
| 15 | + error('Two arguments are required.') |
| 16 | +end |
| 17 | +if ~isstruct(spA) |
| 18 | + spA = array2spArray(spA); |
| 19 | +end |
| 20 | +if any(dim<1) |
| 21 | + error('All entries in ''dim'' must be positive integers.') |
| 22 | +end |
| 23 | + |
| 24 | +nDimA = numel(spA.Size); |
| 25 | +dim(dim>nDimA) = []; |
| 26 | +dim = unique(dim); |
| 27 | +if numel(dim) == nDimA |
| 28 | + % simpler calculation if all dimensions are flipped |
| 29 | + indA = prod(spA.Size) - spA.Ind + 1; |
| 30 | +else |
| 31 | + logDim = zeros(1,nDimA); |
| 32 | + % vector of 1s for flips and 0s for no flips |
| 33 | + logDim(dim) = 1; |
| 34 | + % vector of 1s for flips and -1s for no flips |
| 35 | + mult = logDim; |
| 36 | + mult(logDim==0) = -1; |
| 37 | + % convert to subs |
| 38 | + subA = spInd2spSub(spA); |
| 39 | + % do the flips |
| 40 | + flipSubA = (logDim.*spA.Size - subA + logDim).*mult; |
| 41 | + % convert to linearindex |
| 42 | + indA = spSub2spInd(spA.Size,flipSubA); |
| 43 | +end |
| 44 | + |
| 45 | +% Make the sparse array structure |
| 46 | +spC = struct('Size',spA.Size,'Ind',indA,'Val',spA.Val); |
| 47 | + |
| 48 | +end |
| 49 | + |
0 commit comments