-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspFlip.m
49 lines (44 loc) · 1.35 KB
/
spFlip.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function spC = spFlip(spA,dim)
%SPFLIP Flip order of entries in dimensions specified in 'dim'.
%
% spC = spFlip(spA,dim): Flip the order of entries of the full array,
% represented as a sparse array structure or full array, in the dimensions
% specified in the row vector or scalar 'dim'. The output is a sparse array
% structure.
%
% By Andrew J. Milne, The MARCS Institute, Western Sydney University
%
% See also FLIP.
% If full array, convert to sparse array structure
if nargin < 2
error('Two arguments are required.')
end
if ~isstruct(spA)
spA = array2SpArray(spA);
end
if any(dim<1)
error('All entries in ''dim'' must be positive integers.')
end
nDimA = numel(spA.Size);
dim(dim>nDimA) = [];
dim = unique(dim);
if numel(dim) == nDimA
% simpler calculation if all dimensions are flipped
indA = prod(spA.Size) - spA.Ind + 1;
else
logDim = zeros(1,nDimA);
% vector of 1s for flips and 0s for no flips
logDim(dim) = 1;
% vector of 1s for flips and -1s for no flips
signChange = logDim;
signChange(logDim==0) = -1;
% convert to subs
subA = spInd2SpSub(spA);
% do the flips
flipSubA = (logDim.*spA.Size - subA + logDim).*signChange;
% convert to linearindex
indA = spSub2SpInd(spA.Size,flipSubA);
end
% Make the sparse array structure
spC = struct('Size',spA.Size,'Ind',indA,'Val',spA.Val);
end