annotate scripts/general/shiftdim.m @ 4895:9df0048ae0a1

[project @ 2004-06-03 19:34:07 by jwe]
author jwe
date Thu, 03 Jun 2004 19:34:07 +0000
parents ab5870f984d9
children c08cb1098afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2004 John Eaton, David Bateman
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
2 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
4 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
8 ## any later version.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
9 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
14 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
18 ## 02111-1307, USA.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
19
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{y}} = shiftdim (@var{x}, @var{n})
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
22 ## @deftypefnx {Function File} {[@var{y}, @var{ns}]} = shiftdim (@var{x})
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
23 ## Shifts the dimension of @var{x} by @var{n}, where @var{n} must be
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
24 ## an integer scalar. When @var{n} is negative, the dimensions of
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
25 ## @var{x} are shifted to the left, with the leading dimensions
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
26 ## circulated to the end. If @var{n} is positive, then the dimensions
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
27 ## of @var{x} are shifted to the right, with the @var{n} singleton
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
28 ## dimensions added.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
29 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
30 ## Called with a single argument, @code{shiftdim}, removes the leading
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
31 ## singleton dimensions, returning the number of dimensions removed
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
32 ## in the second output argument @var{ns}.
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
33 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
34 ## For example
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
35 ##
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
36 ## @example
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
37 ## @group
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
38 ## x = ones (1, 2, 3);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
39 ## size (shiftdim (x, -1))
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
40 ## @result{} [2, 3, 1]
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
41 ## size (shiftdim (x, 1))
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
42 ## @result{} [1, 1, 2, 3]
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
43 ## [b, ns] = shiftdim (x);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
44 ## @result{} b = [1, 1, 1; 1, 1, 1]
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
45 ## @result{} ns = 1
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
46 ## @end group
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
47 ## @end example
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
48 ## @end deftypefn
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
49 ## @seealso {reshape, permute, ipermute, circshift, squeeze}
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
50
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
51 function [y, ns] = shiftdim (x, n)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
52
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
53 if (nargin == 1)
4895
9df0048ae0a1 [project @ 2004-06-03 19:34:07 by jwe]
jwe
parents: 4894
diff changeset
54 ## Find the first singleton dimension.
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
55 nd = ndims (x);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
56 orig_dims = size (x);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
57 ns = 1;
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
58 while (ns < nd + 1 && orig_dims(ns) == 1)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
59 ns = ns + 1;
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
60 endwhile
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
61 if (ns > nd)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
62 ns = 1;
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
63 endif
4895
9df0048ae0a1 [project @ 2004-06-03 19:34:07 by jwe]
jwe
parents: 4894
diff changeset
64 y = reshape (x, orig_dims(ns:end));
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
65 ns = ns - 1;
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
66 elseif (nargin == 2)
4895
9df0048ae0a1 [project @ 2004-06-03 19:34:07 by jwe]
jwe
parents: 4894
diff changeset
67 if (! isscalar (n) && floor (n) != n)
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
68 error ("shiftdim: n must be an scalar integer");
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
69 endif
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
70 if (n < 0)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
71 orig_dims = size (x);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
72 singleton_dims = ones (1, -n);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
73 y = reshape (x, [singleton_dims, orig_dims]);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
74 elseif (n > 0)
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
75 ndims = length (size (x));
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
76 y = permute (x, [n+1:ndims, 1:n]);
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
77 else
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
78 y = x;
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
79 endif
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
80 else
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
81 usage ("shiftdim (x, n) or [b, ns] = shiftdim (x)");
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
82 endif
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
83 endfunction