4894
|
1 ## Copyright (C) 2004 John Eaton, David Bateman |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
4894
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
4894
|
18 |
|
19 ## -*- texinfo -*- |
6547
|
20 ## @deftypefn {Function File} {@var{y} =} shiftdim (@var{x}, @var{n}) |
|
21 ## @deftypefnx {Function File} {[@var{y}, @var{ns}] =} shiftdim (@var{x}) |
4894
|
22 ## Shifts the dimension of @var{x} by @var{n}, where @var{n} must be |
5539
|
23 ## an integer scalar. When @var{n} is positive, the dimensions of |
4894
|
24 ## @var{x} are shifted to the left, with the leading dimensions |
5539
|
25 ## circulated to the end. If @var{n} is negative, then the dimensions |
|
26 ## of @var{x} are shifted to the right, with @var{n} leading singleton |
4894
|
27 ## dimensions added. |
|
28 ## |
|
29 ## Called with a single argument, @code{shiftdim}, removes the leading |
|
30 ## singleton dimensions, returning the number of dimensions removed |
|
31 ## in the second output argument @var{ns}. |
|
32 ## |
|
33 ## For example |
|
34 ## |
|
35 ## @example |
|
36 ## @group |
|
37 ## x = ones (1, 2, 3); |
|
38 ## size (shiftdim (x, -1)) |
5984
|
39 ## @result{} [1, 1, 2, 3] |
4894
|
40 ## size (shiftdim (x, 1)) |
5984
|
41 ## @result{} [2, 3] |
4894
|
42 ## [b, ns] = shiftdim (x); |
|
43 ## @result{} b = [1, 1, 1; 1, 1, 1] |
|
44 ## @result{} ns = 1 |
|
45 ## @end group |
|
46 ## @end example |
5642
|
47 ## @seealso {reshape, permute, ipermute, circshift, squeeze} |
4894
|
48 ## @end deftypefn |
|
49 |
|
50 function [y, ns] = shiftdim (x, n) |
|
51 |
5518
|
52 if (nargin < 1 || nargin > 2) |
6046
|
53 print_usage (); |
5518
|
54 endif |
|
55 |
|
56 nd = ndims (x); |
|
57 orig_dims = size (x); |
|
58 |
4894
|
59 if (nargin == 1) |
4895
|
60 ## Find the first singleton dimension. |
5518
|
61 n = 0; |
|
62 while (n < nd && orig_dims(n+1) == 1) |
|
63 n++; |
4894
|
64 endwhile |
5518
|
65 endif |
|
66 |
|
67 if (! isscalar (n) || floor (n) != n) |
|
68 error ("shiftdim: n must be a scalar integer"); |
|
69 endif |
|
70 |
|
71 if (n >= nd) |
|
72 n = rem (n, nd); |
|
73 endif |
|
74 |
|
75 if (n < 0) |
|
76 singleton_dims = ones (1, -n); |
|
77 y = reshape (x, [singleton_dims, orig_dims]); |
|
78 elseif (n > 0) |
5984
|
79 ## We need permute here instead of reshape to shift values in a |
|
80 ## compatible way. |
|
81 y = permute (x, [n+1:ndims(x) 1:n]); |
4894
|
82 else |
5518
|
83 y = x; |
4894
|
84 endif |
5518
|
85 |
|
86 ns = n; |
|
87 |
4894
|
88 endfunction |