]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/flipdim.m
update packages
[CreaPhase.git] / octave_packages / m / general / flipdim.m
1 ## Copyright (C) 2004-2012 David Bateman
2 ## Copyright (C) 2009 VZLU Prague
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} flipdim (@var{x})
22 ## @deftypefnx {Function File} {} flipdim (@var{x}, @var{dim})
23 ## Return a copy of @var{x} flipped about the dimension @var{dim}.
24 ## @var{dim} defaults to the first non-singleton dimension.
25 ## For example:
26 ##
27 ## @example
28 ## @group
29 ## flipdim ([1, 2; 3, 4], 2)
30 ##       @result{}  2  1
31 ##           4  3
32 ## @end group
33 ## @end example
34 ## @seealso{fliplr, flipud, rot90, rotdim}
35 ## @end deftypefn
36
37 ## Author: David Bateman, Jaroslav Hajek
38
39 function y = flipdim (x, dim)
40
41   if (nargin != 1 && nargin != 2)
42     print_usage ();
43   endif
44
45   nd = ndims (x);
46   sz = size (x);
47   if (nargin == 1)
48     ## Find the first non-singleton dimension.
49     (dim = find (sz > 1, 1)) || (dim = 1);
50   elseif (! (isscalar (dim) && isindex (dim)))
51     error ("flipdim: DIM must be a positive integer");
52   endif
53
54   idx(1:max(nd, dim)) = {':'};
55   idx{dim} = size (x, dim):-1:1;
56   y = x(idx{:});
57
58 endfunction
59
60 %!error flipdim ();
61 %!error flipdim (1, 2, 3);
62
63 %!assert (flipdim ([1,2;3,4]), flipdim ([1,2 ; 3,4], 1));
64 %!assert (flipdim ([1,2;3,4], 2), [2,1;4,3]);
65 %!assert (flipdim ([1,2;3,4], 3), [1,2;3,4]);
66
67 ## FIXME -- we need tests for multidimensional arrays.