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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {@var{y}} = shiftdim (@var{x}, @var{n}) |
|
22 ## @deftypefnx {Function File} {[@var{y}, @var{ns}]} = shiftdim (@var{x}) |
|
23 ## Shifts the dimension of @var{x} by @var{n}, where @var{n} must be |
|
24 ## an integer scalar. When @var{n} is negative, the dimensions of |
|
25 ## @var{x} are shifted to the left, with the leading dimensions |
|
26 ## circulated to the end. If @var{n} is positive, then the dimensions |
|
27 ## of @var{x} are shifted to the right, with the @var{n} singleton |
|
28 ## dimensions added. |
|
29 ## |
|
30 ## Called with a single argument, @code{shiftdim}, removes the leading |
|
31 ## singleton dimensions, returning the number of dimensions removed |
|
32 ## in the second output argument @var{ns}. |
|
33 ## |
|
34 ## For example |
|
35 ## |
|
36 ## @example |
|
37 ## @group |
|
38 ## x = ones (1, 2, 3); |
|
39 ## size (shiftdim (x, -1)) |
|
40 ## @result{} [2, 3, 1] |
|
41 ## size (shiftdim (x, 1)) |
|
42 ## @result{} [1, 1, 2, 3] |
|
43 ## [b, ns] = shiftdim (x); |
|
44 ## @result{} b = [1, 1, 1; 1, 1, 1] |
|
45 ## @result{} ns = 1 |
|
46 ## @end group |
|
47 ## @end example |
|
48 ## @end deftypefn |
|
49 ## @seealso {reshape, permute, ipermute, circshift, squeeze} |
|
50 |
|
51 function [y, ns] = shiftdim (x, n) |
|
52 |
|
53 if (nargin == 1) |
4895
|
54 ## Find the first singleton dimension. |
4894
|
55 nd = ndims (x); |
|
56 orig_dims = size (x); |
|
57 ns = 1; |
|
58 while (ns < nd + 1 && orig_dims(ns) == 1) |
|
59 ns = ns + 1; |
|
60 endwhile |
|
61 if (ns > nd) |
|
62 ns = 1; |
|
63 endif |
4895
|
64 y = reshape (x, orig_dims(ns:end)); |
4894
|
65 ns = ns - 1; |
|
66 elseif (nargin == 2) |
4895
|
67 if (! isscalar (n) && floor (n) != n) |
4894
|
68 error ("shiftdim: n must be an scalar integer"); |
|
69 endif |
|
70 if (n < 0) |
|
71 orig_dims = size (x); |
|
72 singleton_dims = ones (1, -n); |
|
73 y = reshape (x, [singleton_dims, orig_dims]); |
|
74 elseif (n > 0) |
|
75 ndims = length (size (x)); |
|
76 y = permute (x, [n+1:ndims, 1:n]); |
|
77 else |
|
78 y = x; |
|
79 endif |
|
80 else |
|
81 usage ("shiftdim (x, n) or [b, ns] = shiftdim (x)"); |
|
82 endif |
|
83 endfunction |