-
Subtraction and negation operator.
Category: Language Fundamentals > Operators and Elementary Operations
Syntax
C = A - B
Subtracts B from A element by element. Same as minus(A, B).
B = -A
Changes the sign of every element. Same as uminus(A).
Description
Sizes are compatible when each dimension is equal or one of them is 1, so a row vector minus a column vector gives a matrix. Subtracting the mean from a matrix, A - mean(A), relies on this.
Integer arithmetic saturates: uint8(5) - uint8(10) is 0. Mixing different integer types is an error, but an integer with a double gives an integer. Logical and char operands are converted to double.
Input arguments
A, B- Numeric, logical or char arrays with compatible sizes.
Output arguments
C- The difference, with the size given by implicit expansion.
Example
[5 7 9] - 1
[1; 2] - [10 20 30]
-[1 -2 3]
uint8(5) - uint8(10)
Output
ans = 1×3
4 6 8
ans = 2×3
-9 -19 -29
-8 -18 -28
ans = 1×3
-1 2 -3
ans = uint8
0