1 |
function B = rotarray(A, n, dim) |
---|
2 |
%ROTARRAY Rotate array or subarrays. |
---|
3 |
% |
---|
4 |
% ROTARRAY(A) is the 90 degree counterclockwise rotation of matrix A. |
---|
5 |
% ROTARRAY(A, N) is the K*90 degree rotation of A, N = 0,+-1,+-2,... |
---|
6 |
% |
---|
7 |
% Example, |
---|
8 |
% A = [ 1 2 3 B = rotarray(A) = [ 3 6 |
---|
9 |
% 4 5 6 ] 2 5 |
---|
10 |
% 1 4 ] |
---|
11 |
% |
---|
12 |
% If A is an ND array, the same operation is performed on all 2-D |
---|
13 |
% slices A(:,:,I,J,K,...). |
---|
14 |
% |
---|
15 |
% ROTARRAY(A, K, DIM), where DIM is a vector with two integers, will |
---|
16 |
% perform the rotation counterclockwise around an axis perpendicular |
---|
17 |
% to a plane through the specified dimensions. The default value for |
---|
18 |
% the dimension vector is [1 2]. |
---|
19 |
% ROTARRAY(A, K, DIM) where DIM is a scalar, is equivalent to |
---|
20 |
% ROTARRAY(A, K, [DIM DIM+1]). |
---|
21 |
% ROTARRAY(A, K, [DIM1 DIM2]) is equivalent to |
---|
22 |
% ROTARRAY(A, 4-K, [DIM2 DIM1]). |
---|
23 |
% |
---|
24 |
% Calls: none |
---|
25 |
% |
---|
26 |
% See also ROT90. |
---|
27 |
|
---|
28 |
% Author: Peter J. Acklam |
---|
29 |
% Time-stamp: 2000-04-28 13:23:45 |
---|
30 |
% E-mail: jacklam@math.uio.no |
---|
31 |
% WWW URL: http://www.math.uio.no/~jacklam |
---|
32 |
|
---|
33 |
nargsin = nargin; |
---|
34 |
error(nargchk(1, 4, nargsin)); |
---|
35 |
|
---|
36 |
% Get N. |
---|
37 |
if (nargsin < 2) | isempty(n) |
---|
38 |
n = 1; |
---|
39 |
else |
---|
40 |
if any(size(n) ~= 1) | (n ~= round(n)) |
---|
41 |
error('N must be a scalar integer.'); |
---|
42 |
end |
---|
43 |
n = n - 4*floor(n/4); % map n to {0,1,2,3} |
---|
44 |
end |
---|
45 |
|
---|
46 |
% Get DIM1. |
---|
47 |
if nargsin < 3 |
---|
48 |
dim1 = 1; |
---|
49 |
else |
---|
50 |
if any(size(dim1) ~= 1) | (dim1 <= 0) | (dim1 ~= round(dim1)) |
---|
51 |
error('DIM1 must be a scalar positive integer.'); |
---|
52 |
end |
---|
53 |
end |
---|
54 |
|
---|
55 |
% Get DIM2. |
---|
56 |
if nargsin < 4 |
---|
57 |
dim2 = dim1 + 1; |
---|
58 |
else |
---|
59 |
if any(size(dim2) ~= 1) | (dim2 <= 0) | (dim2 ~= round(dim2)) |
---|
60 |
error('DIM2 must be a scalar positive integer.'); |
---|
61 |
end |
---|
62 |
end |
---|
63 |
|
---|
64 |
if (n == 0) | ((size(A,dim1) <= 1) & (size(A,dim2) <= 1)) |
---|
65 |
% Special case when output is identical to input. |
---|
66 |
B = A; |
---|
67 |
else |
---|
68 |
% Largest dimension number we have to deal with. |
---|
69 |
nd = max([ ndims(A) dim1 dim2 ]); |
---|
70 |
|
---|
71 |
% Initialize subscript cell array. |
---|
72 |
v = {':'}; |
---|
73 |
v = v(ones(nd,1)); |
---|
74 |
|
---|
75 |
switch n |
---|
76 |
case 1 |
---|
77 |
v{dim2} = size(A,dim2):-1:1; |
---|
78 |
d = 1:nd; |
---|
79 |
d([ dim1 dim2 ]) = [ dim2 dim1 ]; |
---|
80 |
B = permute(A(v{:}), d); |
---|
81 |
case 2 |
---|
82 |
v{dim1} = size(A,dim1):-1:1; |
---|
83 |
v{dim2} = size(A,dim2):-1:1; |
---|
84 |
B = A(v{:}); |
---|
85 |
case 3 |
---|
86 |
v{dim1} = size(A,dim1):-1:1; |
---|
87 |
d = 1:nd; |
---|
88 |
d([ dim1 dim2 ]) = [ dim2 dim1 ]; |
---|
89 |
B = permute(A(v{:}), d); |
---|
90 |
end |
---|
91 |
end |
---|