7017
|
1 ## Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
5012
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5012
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5012
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} flipdim (@var{x}, @var{dim}) |
|
21 ## Return a copy of @var{x} flipped about the dimension @var{dim}. |
5018
|
22 ## For example |
5012
|
23 ## |
|
24 ## @example |
|
25 ## @group |
|
26 ## flipdim ([1, 2; 3, 4], 2) |
|
27 ## @result{} 2 1 |
|
28 ## 4 3 |
|
29 ## @end group |
|
30 ## @end example |
5642
|
31 ## @seealso{fliplr, flipud, rot90, rotdim} |
5012
|
32 ## @end deftypefn |
|
33 |
|
34 ## Author: David Bateman |
|
35 |
|
36 function y = flipdim (x, dim) |
|
37 |
|
38 if (nargin != 1 && nargin != 2) |
6046
|
39 print_usage (); |
5012
|
40 endif |
|
41 |
|
42 nd = ndims (x); |
|
43 sz = size (x); |
|
44 if (nargin == 1) |
|
45 ## Find the first non-singleton dimension. |
|
46 dim = 1; |
|
47 while (dim < nd + 1 && sz(dim) == 1) |
|
48 dim = dim + 1; |
|
49 endwhile |
|
50 if (dim > nd) |
|
51 dim = 1; |
|
52 endif |
|
53 else |
|
54 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && dim < (nd + 1)) |
|
55 error ("flipdim: dim must be an integer and valid dimension"); |
|
56 endif |
|
57 endif |
|
58 |
|
59 idx = cell (); |
|
60 for i = 1:nd |
|
61 idx{i} = 1:sz(i); |
|
62 endfor |
|
63 idx{dim} = sz(dim):-1:1; |
|
64 y = x(idx{:}); |
|
65 |
|
66 endfunction |