Some queries return either 1 or 0 depending on whether all entries in the matrix satisfy some condition. Other queries return a 0-1 matrix where each entry is replaced by whether or not the condition is satisfied. Finally, others check the actual type of the matrix.
The following queries return either 0 or 1:
| Function | Comments |
|---|---|
| isreal(A) | Returns 1 if all entries are real |
| isequal(A, B) | Returns 1 if A(i, j) == B(i, j) for all entries. |
| ismember(x, A) | Returns 1 if x = A(i, j) for some index (i, j). |
>> isreal( [1 2 3 4] )
ans =
1
The following queries map onto matrices:
isfinite isinf isprime| Function | Comments |
|---|---|
| isfinite(x) | Returns 1 if x is not infinity or not-a-number. |
| isnan(x) | Returns 1 if x is not-a-number. |
| isinf(x) | Returns 1 if x is infinite. |
| isprime(x) | Returns 1 if x is a prime number. |
>> isprime( [1 2 3; 4 5 6] )
ans =
0 1 1
0 1 0
The routines isnumeric, islogical, and ischar check the type of a matrix. Any matrix can be converted into the corresponding type by an appropriate function call.
| Type | Function | Comment | Converting to the Given Type |
|---|---|---|---|
| numeric | isnumeric(A) | Does the matrix contain numeric data | double(A) |
| logical | islogical(A) | Is the matrix logical (i.e., can it be used for logical indexing) | logical(A) |
| char | ischar(A) | Does the matrix represent strings | char(A) |
Any logical matrix is also numeric.
>> A = [57.3, 95.2, 35.3]
A =
57.3000 95.2000 35.3000
>> char( A )
ans =
9_#
>> strx = 'hello world'
strx =
hello world
>> double( strx )
ans =
104 101 108 108 111 32 119 111 114 108 100
>> B = rand( 4 ) <= 0.5 % create a 0-1 matrix with approximate 50% 1's
B =
0 0 0 0
1 0 1 0
0 1 0 1
1 1 0 1
>> B = logical( B ) % no visible difference
B =
0 0 0 0
1 0 1 0
0 1 0 1
1 1 0 1
>> C = rand( 4 )
C =
0.9355 0.0579 0.1389 0.2722
0.9169 0.3529 0.2028 0.1988
0.4103 0.8132 0.1987 0.0153
0.8936 0.0099 0.6038 0.7468
>> C(B) % select all entries C(i,j) such that B(i,j) == 1
ans =
0.9169
0.8936
0.8132
0.0099
0.2028
0.0153
0.7468