deltas
Syntax
deltas(X, [n])
Arguments
X is a scalar/vector/matrix/table.
n (optional) is an integer specifying the step to shift when comparing the current element to another element in X. For each element Xi in X, return Xi-Xi-n. The default value is 1, meaning to subtract the current element by the adjacent element at right, i.e., Xi-Xi-1.
Details
- If X is a vector, return a vector containing the subtractions between two elements.
- If X is a matrix, conduct the aforementioned calculation within each column of X. The result is a matrix with the same shape as X.
- If X is a table, conduct the aforementioned calculation within each column of X. The result is a table with the same shape as X.
Examples
x=7 4 5 8 9;
deltas(x);
// output: [,-3,1,3,1]
x=NULL 1 2 NULL 3;
deltas(x);
// output: [,,1,,]
m=matrix(1 3 2 5 6, 0 8 NULL 7 6);
m;
#0 | #1 |
---|---|
1 | 0 |
3 | 8 |
2 | |
5 | 7 |
6 | 6 |
deltas(m);
#0 | #1 |
---|---|
2 | 8 |
-1 | |
3 | |
1 | -1 |
When n is a positive
integer:
m=matrix(1 3 2 5 6, 0 8 NULL 7 6);
a=deltas(m,2)
a;
0 | 1 |
---|---|
1 | |
2 | -1 |
4 |
When n is a negative
integer:
m = 3 4 6 9
r2= deltas(m,-2)
r2; // output: [-3,-5,,]