Mercurial > octave-nkf
comparison scripts/general/flipdim.m @ 5012:ed25bed43409
[project @ 2004-09-21 15:45:48 by jwe]
author | jwe |
---|---|
date | Tue, 21 Sep 2004 15:45:48 +0000 |
parents | |
children | 1c65a8e44ef9 |
comparison
equal
deleted
inserted
replaced
5011:1743bf921773 | 5012:ed25bed43409 |
---|---|
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 | |
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
18 ## 02111-1307, USA. | |
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}. | |
23 ## For exmaple | |
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 | |
32 ## @end deftypefn | |
33 ## @seealso{fliplr, flipud, rot90 and rotdim} | |
34 | |
35 ## Author: David Bateman | |
36 | |
37 function y = flipdim (x, dim) | |
38 | |
39 if (nargin != 1 && nargin != 2) | |
40 usage ("flipdim (x, dim)"); | |
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 |