Check whether all the elements are true.
v = all(M) v = all(M,dim) b = all(v)
all performs a logical AND on the elements of the columns of a matrix, or the elements of a vector. If a second argument dim is provided, the operation is performed column by column if dim is 1, and row by row if it is 2.
all can be omitted if its result would be used in the statement if or while, because these statements consider a matrix to be true if all the elements of the matrix are nonzero.
all([1,2,3] == 2) false all([1,2,3] > 0) true
Check whether any element is true.
v = any(M) v = any(M,dim) b = any(v)
any performs a logical OR on the elements of the columns of a matrix, or the elements of a vector. If a second argument dim is provided, the operation is performed column by column if dim is 1, and row by row if it is 2.
any([1,2,3] == 2) true any([1,2,3] > 5) false
Bitwise AND.
c = bitand(a, b)
The input arguments are converted to 32-bit unsigned integers; each bit of the result is the binary AND of the corresponding bits of the inputs.
The inputs can be scalar, matrices of the same size, or a scalar and a matrix.
bitand(1,3) 1 bitand(1:6,1) 1 0 1 0 1 0
Bit complement (bitwise NOT).
b = bitcmp(a, n)
The first input argument is converted to a unsigned integer of n bits, and these bits are inverted.
The inputs can be scalar, matrices of the same size, or a mix of both.
bitcmp(1,4) 14 bitcmp(0, 1:8) 1 3 7 15 31 63 127 255
Bit extraction.
b = bitget(a, n)
The first input argument is converted to 32-bit unsigned integers; the result is the n:th bit, where 1 is the least significant bit and 32 is the most significant one.
The inputs can be scalar, matrices of the same size, or a scalar and a matrix.
bitget(123,5) 1 bitget(7, 1:8) 1 1 1 0 0 0 0 0
bitand, bitor, bitxor, bitshift
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, matrices of the same size, or a scalar and a matrix.
bitor(1,2) 3 bitor(1:6,1) 1 3 3 5 5 7
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, matrices 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, matrices of the same size, or a scalar and a matrix.
bitxor(1,3) 2 bitxor(1:6,1) 0 3 2 5 4 7
Boolean constant false.
b = false
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 a matrix) are an important exception.
false false islogical(false) true
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 a matrix. Logical values are obtained with comparison operators, logical operators, test functions, and the function logical.
islogical(eye(10)) false islogical(~eye(10)) true
Transform a number into a boolean.
b = logical(x)
logical(x) sets the logical flag associated to x. The logical flag has an effect only when x is used to access the elements of a variable, and in the argument of islogical or ===.
a=1:3; a([1,0,1]) Index out of range a=1:3; a(logical([1,0,1])) 1 3
islogical, double, char, setstr, operator ()
Boolean constant true.
b = true
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 a matrix) are an important exception.
true true islogical(true) true
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