difference
Syntax
difference(X)
Details
Returns the value of the last element of X minus the first element.
- If X is a scalar, returns 0.
- If X is a matrix, calculates the difference between the last and first elements of each column and returns a vector.
- If X is a table, applies the same calculation to each column and returns a one-row table.
Parameters
X can be a scalar, vector, matrix, or table. It supports data types that can participate in subtraction, such as numeric, Boolean, and temporal types.
Returns
The return type is determined by the result type of subtracting the first element from the last element of X.
- For scalar or vector input, returns a scalar.
- For matrix input, returns a vector.
- For table input, returns a one-row table.
Examples
difference(2 4 2);
// output: 0
difference(12.3 15.6 17.8);
// output: 5.5
difference(278);
// output: 0
Matrix input:
m = matrix(1 2 3, 10 20 30)
difference(m)
// output: [2,20]
Table input:
t = table(1 2 3 as a, 10 20 30 as b)
difference(t);
Output:
| a | b |
|---|---|
| 2 | 20 |
