annotate scripts/general/shiftdim.m @ 20511:eca5aa3225f4

imshow.m: Add support for 'parent' property (bug #45473). * imshow.m: Add cell variable prop_val_args{:} when calling image or imagesc. Populate prop_val_args with {"parent", hparent} when "parent" argument given to imshow. Remove code commented out in 2015/05/1 which has not caused any problems. Add warning that arguments "border" and "reduce" are not supported.
author Rik <rik@octave.org>
date Sun, 05 Jul 2015 13:55:56 -0700
parents 7503499a252b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19898
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 2004-2015 John Eaton and David Bateman
4894
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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
8 ## your option) any later version.
4894
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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
18
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
20 ## @deftypefn {Function File} {@var{y} =} shiftdim (@var{x}, @var{n})
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6046
diff changeset
21 ## @deftypefnx {Function File} {[@var{y}, @var{ns}] =} shiftdim (@var{x})
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
22 ## Shift the dimensions of @var{x} by @var{n}, where @var{n} must be
20368
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
23 ## an integer scalar.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
24 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
25 ## When @var{n} is positive, the dimensions of @var{x} are shifted to the left,
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
26 ## with the leading dimensions circulated to the end. If @var{n} is negative,
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
27 ## then the dimensions of @var{x} are shifted to the right, with @var{n}
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
28 ## leading singleton dimensions added.
4894
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
20368
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
31 ## singleton dimensions, returning the number of dimensions removed in the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 20038
diff changeset
32 ## second output argument @var{ns}.
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
33 ##
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
34 ## For example:
4894
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))
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
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))
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
42 ## @result{} [2, 3]
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
43 ## [b, ns] = shiftdim (x)
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
44 ## @result{} b = [1, 1, 1; 1, 1, 1]
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
45 ## @result{} ns = 1
4894
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
20038
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19898
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)
12716
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
61 ## Find the first non-singleton dimension.
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
62 (n = find (orig_dims != 1, 1) - 1) || (n = nd);
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
63 elseif (! (isscalar (n) && n == fix (n)))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
64 error ("shiftdim: N must be a scalar integer");
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
65 endif
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
66
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
67 if (n >= nd)
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
68 n = rem (n, nd);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
69 endif
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
70
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
71 if (n < 0)
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
72 singleton_dims = ones (1, -n);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
73 y = reshape (x, [singleton_dims, orig_dims]);
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
74 elseif (n > 0)
5984
82a73f5dadd9 [project @ 2006-09-12 02:23:37 by jwe]
jwe
parents: 5642
diff changeset
75 ## 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
76 ## compatible way.
12716
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
77 y = permute (x, [n+1:nd 1:n]);
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
78 else
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
79 y = x;
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
80 endif
5518
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
81
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
82 ns = n;
a9bd6c31751f [project @ 2005-10-29 04:26:38 by jwe]
jwe
parents: 5307
diff changeset
83
4894
ab5870f984d9 [project @ 2004-06-03 19:32:02 by jwe]
jwe
parents:
diff changeset
84 endfunction
12716
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
85
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
86
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
87 %!test
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
88 %! x = rand (1, 1, 4, 2);
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
89 %! [y, ns] = shiftdim (x);
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
90 %! assert (size (y), [4 2]);
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
91 %! assert (ns, 2);
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
92 %! assert (shiftdim (y, -2), x);
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
93 %! assert (size (shiftdim (x, 2)), [4 2]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
94
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
95 %!assert (size (shiftdim (rand (0, 1, 2))), [0 1 2])
12716
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
96
20038
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19898
diff changeset
97 ## Test input validation
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
98 %!error (shiftdim ())
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
99 %!error (shiftdim (1,2,3))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
100 %!error (shiftdim (1, ones (2)))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
101 %!error (shiftdim (1, 1.5))
12716
6835bc816ef2 shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
102