5012
|
1 ## Copyright (C) 2004 David Bateman |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
5012
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} flipdim (@var{x}, @var{dim}) |
|
22 ## Return a copy of @var{x} flipped about the dimension @var{dim}. |
5018
|
23 ## For example |
5012
|
24 ## |
|
25 ## @example |
|
26 ## @group |
|
27 ## flipdim ([1, 2; 3, 4], 2) |
|
28 ## @result{} 2 1 |
|
29 ## 4 3 |
|
30 ## @end group |
|
31 ## @end example |
5642
|
32 ## @seealso{fliplr, flipud, rot90, rotdim} |
5012
|
33 ## @end deftypefn |
|
34 |
|
35 ## Author: David Bateman |
|
36 |
|
37 function y = flipdim (x, dim) |
|
38 |
|
39 if (nargin != 1 && nargin != 2) |
6046
|
40 print_usage (); |
5012
|
41 endif |
|
42 |
|
43 nd = ndims (x); |
|
44 sz = size (x); |
|
45 if (nargin == 1) |
|
46 ## Find the first non-singleton dimension. |
|
47 dim = 1; |
|
48 while (dim < nd + 1 && sz(dim) == 1) |
|
49 dim = dim + 1; |
|
50 endwhile |
|
51 if (dim > nd) |
|
52 dim = 1; |
|
53 endif |
|
54 else |
|
55 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && dim < (nd + 1)) |
|
56 error ("flipdim: dim must be an integer and valid dimension"); |
|
57 endif |
|
58 endif |
|
59 |
|
60 idx = cell (); |
|
61 for i = 1:nd |
|
62 idx{i} = 1:sz(i); |
|
63 endfor |
|
64 idx{dim} = sz(dim):-1:1; |
|
65 y = x(idx{:}); |
|
66 |
|
67 endfunction |