]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/xor.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / xor.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Mapping Function} {@var{z} =} xor (@var{x}, @var{y})
21 ## Return the `exclusive or' of the entries of @var{x} and @var{y}.
22 ## For boolean expressions @var{x} and @var{y},
23 ## @code{xor (@var{x}, @var{y})} is true if and only if one of @var{x} or
24 ## @var{y} is true.  Otherwise, for @var{x} and @var{y} both true or both
25 ## false, @code{xor} returns false.
26 ##
27 ## The truth table for the xor operation is
28 ##
29 ## @multitable @columnfractions 0.44 .03 .05 .03 0.44
30 ## @item @tab @var{x} @tab @var{y} @tab @var{z} @tab
31 ## @item @tab 0 @tab 0 @tab 0 @tab
32 ## @item @tab 1 @tab 0 @tab 1 @tab
33 ## @item @tab 0 @tab 1 @tab 1 @tab
34 ## @item @tab 1 @tab 1 @tab 0 @tab
35 ## @end multitable
36 ##
37 ## @seealso{and, or, not}
38 ## @end deftypefn
39
40 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
41 ## Created: 16 September 1994
42 ## Adapted-By: jwe
43
44 function z = xor (x, y)
45
46   if (nargin == 2)
47     if (isscalar (x) || isscalar (y) || size_equal (x, y))
48       ## Typecast to logicals is necessary for other numeric types.
49       z = logical (x) != logical (y);
50     else
51       try
52         z = bsxfun (@xor, x, y);
53       catch
54         error ("xor: X and Y must be of compatible size or scalars");
55       end_try_catch
56     endif
57   else
58     print_usage ();
59   endif
60
61 endfunction
62
63 %!assert((xor ([1, 1, 0, 0], [0, 1, 0, 1]) == [1, 0, 0, 1]
64 %! && xor ([i, i, 0, 0], [1, 0, 1, 0]) == [0, 1, 1, 0]));
65
66 %!assert(all (all (xor (eye (2), fliplr (eye (2))) == ones (2))));
67
68 %!error xor ();
69
70 %!error xor (1, 2, 3);
71