Check whether all the elements are true.
v = all(A) v = all(A,dim) b = all(v)
all(A) performs a logical AND on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension.
all can be omitted if its result is used by if or while, because these statements consider an array to be true if all its elements are nonzero.
all([1,2,3] == 2) false all([1,2,3] > 0) true
Check whether any element is true.
v = any(A) v = any(A,dim) b = any(v)
any(A) performs a logical OR on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension.
any([1,2,3] == 2) true any([1,2,3] > 5) false
Check whether all the corresponding bits are true.
v = bitall(A) v = bitall(A,dim) b = bitall(v)
bitall(A) performs a bitwise AND on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension. A can be a double or an integer array. For double arrays, bitall uses the 32 least-significant bits.
bitall([5, 3]) 1 bitall([7uint8, 6uint8; 3uint8, 6uint8], 2) 2x1 uint8 array 6 2
Bitwise AND.
c = bitand(a, b)
Each bit of the result is the binary AND of the corresponding bits of the inputs.
The inputs can be scalar, arrays of the same size, or a scalar and an array. If the input arguments are of type double, so is the result, and the operation is performed on 32 bits.
bitand(1,3) 1 bitand(1:6,1) 1 0 1 0 1 0 bitand(7uint8, 1234int16) 2int16
Check whether any of the corresponding bits is true.
v = bitany(A) v = bitany(A,dim) b = bitany(v)
bitany(A) performs a bitwise OR on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension. A can be a double or an integer array. For double arrays, bitany uses the 32 least-significant bits.
bitany([5, 3]) 7 bitany([0uint8, 6uint8; 3uint8, 6uint8], 2) 2x1 uint8 array 6 7
Bit complement (bitwise NOT).
b = bitcmp(i) b = bitcmp(a, n)
bitcmp(i) gives the 1-complement (bitwise NOT) of the integer i.
bitcmp(a,n), where a is an integer or a double, gives the 1-complement of the n least-significant bits. The result has the same type as a.
The inputs can be scalar, arrays of the same size, or a scalar and an array. If a is of type double, so is the result, and the operation is performed on at most 32 bits.
bitcmp(1,4) 14 bitcmp(0, 1:8) 1 3 7 15 31 63 127 255 bitcmp([0uint8, 1uint8, 255uint8]) 1x3 uint8 array 255 254 0
Bit extraction.
b = bitget(a, n)
bitget(a, n) gives the n:th bit of integer a. a can be an integer or a double. The result has the same type as a. n=1 corresponds to the least significant bit.
The inputs can be scalar, arrays of the same size, or a scalar and an array. If a is of type double, so is the result, and n is limited to 32.
bitget(123,5) 1 bitget(7, 1:8) 1 1 1 0 0 0 0 0 bitget(5uint8, 2) 0uint8
Bitwise OR.
c = bitor(a, b)
The input arguments are converted to 32-bit unsigned integers; each bit of the result is the binary OR of the corresponding bits of the inputs.
The inputs can be scalar, arrays of the same size, or a scalar and an array. If the input arguments are of type double, so is the result, and the operation is performed on 32 bits.
bitor(1,2) 3 bitor(1:6,1) 1 3 3 5 5 7 bitor(7uint8, 1234int16) 1239int16
bitand, bitxor, bitany, bitget
Bit assignment.
b = bitset(a, n) b = bitset(a, n, v)
bitset(a,n) sets the n:th bit of integer a to 1. a can be an integer or a double. The result has the same type as a. n=1 corresponds to the least significant bit. With 3 input arguments, bitset(a,n,v) sets the bit to 1 if v is nonzero, or clears it if v is zero.
The inputs can be scalar, arrays of the same size, or a mix of them. If a is of type double, so is the result, and n is limited to 32.
bitset(123,10) 635 bitset(123, 1, 0) 122 bitset(7uint8, 1:8) 1x8 uint8 array 7 7 7 15 23 39 71 135
bitget, bitand, bitor, bitxor, bitshift
Bit shift.
b = bitshift(a, shift)
b = bitshift(a, shift, n)
The first input argument is converted to a 32-bit unsigned integer, and shifted by shift bits, to the left if shift is positive or to the right if it is negative. With a third argument n, only n bits are retained.
The inputs can be scalar, arrays of the same size, or a mix of both.
bitshift(1,3) 8 bitshift(8, -2:2) 2 4 8 16 32 bitshift(15, 0:3, 4) 15 14 12 8
Bitwise exclusive OR.
c = bitxor(a, b)
The input arguments are converted to 32-bit unsigned integers; each bit of the result is the binary exclusive OR of the corresponding bits of the inputs.
The inputs can be scalar, arrays of the same size, or a scalar and an array.
bitxor(1,3) 2 bitxor(1:6,1) 0 3 2 5 4 7 bitxor(7uint8, 1234int16) 1237int16
Boolean constant false.
b = false B = false(n) B = false(n1, n2, ...) B = false([n1, n2, ...])
The boolean constant false can be used to set the value of a variable. It is equivalent to logical(0). The constant 0 is equivalent in many cases; indices (to get or set the elements of an array) are an important exception.
With input arguments, false builds a logical array whose elements are false. The size of the array is specified by one integer for a square matrix, or several integers (either as separate arguments or in a vector) for an array of any size.
false false islogical(false) true false(2,3) F F F F F F
Conversion to Gray code.
g = graycode(n)
graycode(n) converts the integer number n to Gray code. The argument n can be an integer number of class double (converted to an unsigned integer) or any integer type. If it is an array, conversion is performed on each element. The result has the same type and size as the input.
Gray code is an encoding which maps each integer of s bits to another integer of s bits, such that two consecutive codes (i.e. graycode(n) and graycode(n+1) for any n) have only one bit which differs.
graycode(0:7) 0 1 3 2 6 7 5 4
Conversion from Gray code.
n = igraycode(g)
igraycode(n) converts the Gray code g to the corresponding integer. It is the inverse of graycode. The argument n can be an integer number of class double (converted to an unsigned integer) or any integer type. If it is an array, conversion is performed on each element. The result has the same type and size as the input.
igraycode(graycode(0:7)) 0 1 2 3 4 5 6 7
Test for a boolean object.
b = islogical(obj)
islogical(obj) is true if obj is a logical value, and false otherwise. The result is always a scalar, even if obj is an array. Logical values are obtained with comparison operators, logical operators, test functions, and the function logical.
islogical(eye(10)) false islogical(~eye(10)) true
logical, isnumeric, isinteger, ischar
Transform a number into a boolean.
B = logical(A)
logical(x) converts array or number A to logical (boolean) type. All nonzero elements of A are converted to true, and zero elements to false.
Logical values are stored as 0 for false or 1 for true in unsigned 8-bit integers. They differ from the uint8 type when they are used to select the elements of an array or list.
a=1:3; a([1,0,1]) Index out of range a=1:3; a(logical([1,0,1])) 1 3
islogical, uint8, double, char, setstr, operator ()
Boolean constant true.
b = true B = true(n) B = true(n1, n2, ...) B = true([n1, n2, ...])
The boolean constant true can be used to set the value of a variable. It is equivalent to logical(1). The constant 1 is equivalent in many cases; indices (to get or set the elements of an array) are an important exception.
With input arguments, true builds a logical array whose elements are true. The size of the array is specified by one integer for a square matrix, or several integers (either as separate arguments or in a vector) for an array of any size.
true true islogical(true) true true(2) T T T T
Exclusive or.
b3 = xor(b1,b2)
xor(b1,b2) performs the exclusive or operation between the corresponding elements of b1 and b2. b1 and b2 must have the same size or one of them must be a scalar.
xor([false false true true],[false true false true]) F T T F xor(pi,8) false