Mercurial > hg > octave-lyh
annotate scripts/general/logical.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 935be827eaf8 |
children |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1997, 1998, 2000, 2005, 2006, 2007, 2008 John W. Eaton |
2823 | 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. | |
2823 | 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/>. | |
2823 | 18 |
3449 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} logical (@var{arg}) | |
21 ## Convert @var{arg} to a logical value. For example, | |
2823 | 22 ## |
3449 | 23 ## @example |
24 ## logical ([-1, 0, 1]) | |
25 ## @end example | |
2823 | 26 ## |
3449 | 27 ## @noindent |
2823 | 28 ## is equivalent to |
29 ## | |
3449 | 30 ## @example |
31 ## [-1, 0, 1] != 0 | |
32 ## @end example | |
33 ## @end deftypefn | |
2823 | 34 |
35 ## Author: jwe | |
36 | |
37 function y = logical (x) | |
38 | |
39 if (nargin == 1) | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
40 if (islogical (x)) |
3674 | 41 y = x; |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
42 elseif (isempty (x)) |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
43 y = zeros (size (x), "logical"); |
3674 | 44 elseif (isnumeric (x)) |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
45 if (any (isnan (x(:)))) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
46 error ("invalid conversion from NaN to logical"); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
47 else |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
48 y = x != 0; |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
49 endif |
3225 | 50 else |
3674 | 51 error ("logical not defined for type `%s'", typeinfo (x)); |
3225 | 52 endif |
2823 | 53 else |
6046 | 54 print_usage (); |
2823 | 55 endif |
56 | |
57 endfunction | |
7652
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
58 |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
59 %!assert (logical ([]), zeros ([0, 0], "logical")); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
60 %!assert (logical (zeros (2, 0)), zeros ([2, 0], "logical")); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
61 %!assert (logical (0), false); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
62 %!assert (logical (13), true); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
63 %!assert (logical (-13), true); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
64 %!assert (logical (int8 (13)), true); |
b5731e43283a
ismember: correctly size idx output for empty args
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
65 %!assert (logical (int8 (-13)), true); |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
66 %!assert (logical ([-1, 0, 1, Inf]), [-1, 0, 1, Inf] != 0); |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
67 %!error (logical ([-1, 0, 1, NaN, Inf])) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7652
diff
changeset
|
68 %!error (logical (NaN)) |