annotate scripts/general/flipdim.m @ 10692:b32a0214a464

Use > 1 test to find first non-singleton dimension rather than != 1.
author Rik <octave@nomad.inbox5.com>
date Wed, 09 Jun 2010 07:29:33 -0700
parents 1b290ce305fb
children 01f1643dfbb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2004, 2005, 2006, 2007 David Bateman
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
3 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
5 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
7 ## 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: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
10 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
15 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
16 ## 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: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
19
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
21 ## @deftypefn {Function File} {} flipdim (@var{x})
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
22 ## @deftypefnx {Function File} {} flipdim (@var{x}, @var{dim})
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
23 ## Return a copy of @var{x} flipped about the dimension @var{dim}.
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
24 ## @var{dim} defaults to the first non-singleton dimension.
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
25 ## For example,
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
26 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
27 ## @example
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
28 ## @group
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
29 ## flipdim ([1, 2; 3, 4], 2)
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
30 ## @result{} 2 1
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
31 ## 4 3
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
32 ## @end group
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
33 ## @end example
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
34 ## @seealso{fliplr, flipud, rot90, rotdim}
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
35 ## @end deftypefn
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
36
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
37 ## Author: David Bateman, Jaroslav Hajek
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
38
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
39 function y = flipdim (x, dim)
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
40
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
41 if (nargin != 1 && nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
42 print_usage ();
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
43 endif
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
44
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
45 nd = ndims (x);
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
46 if (nargin == 1)
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
47 ## Find the first non-singleton dimension.
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
48 dim = find (size (x) > 1, 1);
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
49 if (isempty (dim))
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
50 dim = 1;
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
51 endif
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
52 else
9510
1b290ce305fb add omitted check in flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 9508
diff changeset
53 if (! (isscalar (dim) && isindex (dim, nd)))
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
54 error ("flipdim: DIM must be an integer and a valid dimension");
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
55 endif
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
56 endif
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
57
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
58 idx(1:nd) = {':'};
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
59 idx{dim} = size (x, dim):-1:1;
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
60 y = x(idx{:});
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
61
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
62 endfunction