comparison scripts/general/flipdim.m @ 9508:e5e4e404a59d

simplify flipdim
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 07 Aug 2009 08:46:51 +0200
parents a1dbe9d80eee
children 1b290ce305fb
comparison
equal deleted inserted replaced
9507:b096d11237be 9508:e5e4e404a59d
1 ## Copyright (C) 2004, 2005, 2006, 2007 David Bateman 1 ## Copyright (C) 2004, 2005, 2006, 2007 David Bateman
2 ## Copyright (C) 2009 VZLU Prague
2 ## 3 ##
3 ## This file is part of Octave. 4 ## This file is part of Octave.
4 ## 5 ##
5 ## Octave is free software; you can redistribute it and/or modify it 6 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by 7 ## under the terms of the GNU General Public License as published by
29 ## @end group 30 ## @end group
30 ## @end example 31 ## @end example
31 ## @seealso{fliplr, flipud, rot90, rotdim} 32 ## @seealso{fliplr, flipud, rot90, rotdim}
32 ## @end deftypefn 33 ## @end deftypefn
33 34
34 ## Author: David Bateman 35 ## Author: David Bateman, Jaroslav Hajek
35 36
36 function y = flipdim (x, dim) 37 function y = flipdim (x, dim)
37 38
38 if (nargin != 1 && nargin != 2) 39 if (nargin != 1 && nargin != 2)
39 print_usage (); 40 print_usage ();
40 endif 41 endif
41 42
42 nd = ndims (x); 43 nd = ndims (x);
43 sz = size (x);
44 if (nargin == 1) 44 if (nargin == 1)
45 ## Find the first non-singleton dimension. 45 ## Find the first non-singleton dimension.
46 dim = 1; 46 dim = find (size (x) != 1, 1);
47 while (dim < nd + 1 && sz(dim) == 1) 47 if (isempty (dim))
48 dim = dim + 1;
49 endwhile
50 if (dim > nd)
51 dim = 1; 48 dim = 1;
52 endif 49 endif
53 else 50 else
54 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && dim < (nd + 1)) 51 if (! isindex (dim, nd))
55 error ("flipdim: dim must be an integer and valid dimension"); 52 error ("flipdim: dim must be an integer and valid dimension");
56 endif 53 endif
57 endif 54 endif
58 55
59 idx = cell (); 56 idx(1:nd) = {':'};
60 for i = 1:nd 57 idx{dim} = size (x, dim):-1:1;
61 idx{i} = 1:sz(i);
62 endfor
63 idx{dim} = sz(dim):-1:1;
64 y = x(idx{:}); 58 y = x(idx{:});
65 59
66 endfunction 60 endfunction