annotate scripts/general/flip.m @ 19783:40b0f41a7100

Print black&white when using -dps device with gnuplot (bug #43975). * __go_draw_axes__.m: Check the value of mono before adding cmap to plot stream.
author Rik <rik@octave.org>
date Sun, 18 Jan 2015 10:38:11 -0800
parents cbce5d1bcaf9
children 4197fc428c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 14363
diff changeset
1 ## Copyright (C) 2004-2013 David Bateman
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
3 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
5 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
10 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
15 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
19
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
21 ## @deftypefn {Function File} {} flip (@var{x})
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
22 ## @deftypefnx {Function File} {} flip (@var{x}, @var{dim})
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
23 ## Flip array across specific dimension.
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
24 ##
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
25 ## Return a copy of @var{x} flipped about the dimension @var{dim}.
10692
b32a0214a464 Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents: 9510
diff changeset
26 ## @var{dim} defaults to the first non-singleton dimension.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10790
diff changeset
27 ## For example:
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
28 ##
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
29 ## @example
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
30 ## @group
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
31 ## flip ([1 2 3 4])
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
32 ## @result{} 4 3 2 1
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
33 ##
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
34 ## flip ([1; 2; 3; 4])
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
35 ## @result{} 4
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
36 ## 3
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
37 ## 2
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
38 ## 1
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
39 ##
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
40 ## flip ([1 2; 3 4])
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
41 ## @result{} 3 4
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
42 ## 1 2
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
43 ##
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
44 ## flip ([1 2; 3 4], 2)
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
45 ## @result{} 2 1
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
46 ## 4 3
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
47 ## @end group
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
48 ## @end example
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
49 ##
19346
76baa2d10abb flip.m: Add input validation test.
Rik <rik@octave.org>
parents: 19318
diff changeset
50 ## @seealso{fliplr, flipud, rot90, rotdim, permute, transpose}
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
51 ## @end deftypefn
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
52
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
53 ## Author: David Bateman, Jaroslav Hajek
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
54
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
55 function y = flip (x, dim)
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
56
19346
76baa2d10abb flip.m: Add input validation test.
Rik <rik@octave.org>
parents: 19318
diff changeset
57 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
58 print_usage ();
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
59 endif
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
60
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
61 nd = ndims (x);
12796
886256714823 codesprint: Correct missing comma in tests for flipdim.m
Rik <octave@nomad.inbox5.com>
parents: 12795
diff changeset
62 sz = size (x);
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
63 if (nargin == 1)
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
64 ## Find the first non-singleton dimension.
12674
9493880928c8 Use common idiom in m-files for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
65 (dim = find (sz > 1, 1)) || (dim = 1);
10790
01f1643dfbb1 fix flipdim with trailing singleton dims
Jaroslav Hajek <highegg@gmail.com>
parents: 10692
diff changeset
66 elseif (! (isscalar (dim) && isindex (dim)))
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
67 error ("flip: DIM must be a positive integer");
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
68 endif
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
69
10790
01f1643dfbb1 fix flipdim with trailing singleton dims
Jaroslav Hajek <highegg@gmail.com>
parents: 10692
diff changeset
70 idx(1:max(nd, dim)) = {':'};
9508
e5e4e404a59d simplify flipdim
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
71 idx{dim} = size (x, dim):-1:1;
5012
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
72 y = x(idx{:});
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
73
ed25bed43409 [project @ 2004-09-21 15:45:48 by jwe]
jwe
parents:
diff changeset
74 endfunction
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12674
diff changeset
75
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12674
diff changeset
76
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
77 %!assert (flip ([1 2; 3 4], 2), [2 1; 4 3])
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
78 %!assert (flip ([1 2; 3 4], 3), [1 2; 3 4])
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
79
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
80 ## Test defaults
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
81 %!assert (flip ([1 2 3 4]), [4 3 2 1])
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
82 %!assert (flip ([1 2 3 4].'), [4 3 2 1].')
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
83 %!assert (flip ([1 2; 3 4]), flip ([1 2 ; 3 4], 1))
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12674
diff changeset
84
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
85 ## Test NDArrays
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
86 %!test
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
87 %! a(1:2,1:2,1) = [1 2; 3 4];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
88 %! a(1:2,1:2,2) = [5 6; 7 8];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
89 %! b(1:2,1:2,1) = [5 6; 7 8];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
90 %! b(1:2,1:2,2) = [1 2; 3 4];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
91 %! assert (flip (a, 3), b)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
92
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
93 %!test
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
94 %! a = b = zeros (2, 2, 1, 2);
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
95 %! a(1:2,1:2,:,1) = [1 2; 3 4];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
96 %! a(1:2,1:2,:,2) = [5 6; 7 8];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
97 %! b(1:2,1:2,:,1) = [5 6; 7 8];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
98 %! b(1:2,1:2,:,2) = [1 2; 3 4];
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
99 %! assert (flip (a, 3), a)
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
100 %! assert (flip (a, 4), b)
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
101 %! assert (flip (a, 5), a)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
102
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
103 %!error flip ()
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
104 %!error flip (1, 2, 3)
19346
76baa2d10abb flip.m: Add input validation test.
Rik <rik@octave.org>
parents: 19318
diff changeset
105 %!error <DIM must be a positive integer> flip (magic (3), -1)
19318
995df67fc912 Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents: 19047
diff changeset
106