annotate scripts/general/shiftdim.m @ 6547:4fb053f24fd6

[project @ 2007-04-19 21:47:40 by jwe]
author jwe
date Thu, 19 Apr 2007 21:47:41 +0000
parents 34f96dd5441b
children 93c65f2a5668
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
5307
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5053
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5053
diff changeset
18 ## 02110-1301, USA.
4894
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 -*-
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6046
diff changeset
21 ## @deftypefn {Function File} {@var{y} =} shiftdim (@var{x}, @var{n})
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6046
diff changeset
22 ## @deftypefnx {Function File} {[@var{y}, @var{ns}] =} shiftdim (@var{x})
4894
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
5539
b800ae36fc6a [project @ 2005-11-16 18:45:32 by jwe]
jwe
parents: 5518
diff changeset
24 ## an integer scalar. When @var{n} is positive, the dimensions of
4894
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
5539
b800ae36fc6a [project @ 2005-11-16 18:45:32 by jwe]
jwe
parents: 5518
diff changeset
26 ## circulated to the end. If @var{n} is negative, then the dimensions
b800ae36fc6a [project @ 2005-11-16 18:45:32 by jwe]
jwe
parents: 5518
diff changeset
27 ## of @var{x} are shifted to the right, with @var{n} leading singleton
4894
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))
5984
82a73f5dadd9 [project @ 2006-09-12 02:23:37 by jwe]
jwe
parents: 5642
diff changeset
40 ## @result{} [1, 1, 2, 3]
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
41 ## size (shiftdim (x, 1))
5984
82a73f5dadd9 [project @ 2006-09-12 02:23:37 by jwe]
jwe
parents: 5642
diff changeset
42 ## @result{} [2, 3]
4894
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
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5539
diff changeset
48 ## @seealso {reshape, permute, ipermute, circshift, squeeze}
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
49 ## @end deftypefn
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
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
53 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5984
diff changeset
54 print_usage ();
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
55 endif
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
56
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
57 nd = ndims (x);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
58 orig_dims = size (x);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
59
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
60 if (nargin == 1)
4895
9df0048ae0a1 [project @ 2004-06-03 19:34:07 by jwe]
jwe
parents: 4894
diff changeset
61 ## Find the first singleton dimension.
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
62 n = 0;
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
63 while (n < nd && orig_dims(n+1) == 1)
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
64 n++;
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
65 endwhile
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
66 endif
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
67
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
68 if (! isscalar (n) || floor (n) != n)
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
69 error ("shiftdim: n must be a scalar integer");
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
70 endif
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
71
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
72 if (n >= nd)
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
73 n = rem (n, nd);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
74 endif
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
75
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
76 if (n < 0)
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
77 singleton_dims = ones (1, -n);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
78 y = reshape (x, [singleton_dims, orig_dims]);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
79 elseif (n > 0)
5984
82a73f5dadd9 [project @ 2006-09-12 02:23:37 by jwe]
jwe
parents: 5642
diff changeset
80 ## We need permute here instead of reshape to shift values in a
82a73f5dadd9 [project @ 2006-09-12 02:23:37 by jwe]
jwe
parents: 5642
diff changeset
81 ## compatible way.
82a73f5dadd9 [project @ 2006-09-12 02:23:37 by jwe]
jwe
parents: 5642
diff changeset
82 y = permute (x, [n+1:ndims(x) 1:n]);
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
83 else
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
84 y = x;
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
85 endif
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
86
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
87 ns = n;
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
88
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
89 endfunction