cut
Syntax
cut(X, size|cutPositions)
Arguments
X is a scalar/vector/matrix/table.
size is a positive integer that must be no greater than the size of X.
cutPositions is a vector with increasing elements, which is used to specify the starting position of each vector in the result.
Details
This function divides X based on the specified size or cutPositions, and returns a tuple:
When X is a scalar, size can only be specified as 1.
When X is a vector:
if size is specified, it divides X into a list of scalars (size=1) or vectors (size>1) of length size.
if cutPositions is specified, it divides X into a list of vectors at the specified positions.
When X is a matrix (table):
if size is specified, it divides X into several matrices (tables) with size columns (rows).
if cutPositions is specified, it divides X into several matrices (tables) at the specified positions.
Refer to function flatten for the reverse operation.
Examples
$ a=1..10;
$ cut(a,2);
([1,2],[3,4],[5,6],[7,8],[9,10])
$ cut(a,3);
([1,2,3],[4,5,6],[7,8,9],[10])
$ cut(a,9);
([1,2,3,4,5,6,7,8,9],[10])
$ b = cut(a,2);
$ b;
([1,2],[3,4],[5,6],[7,8],[9,10])
$ flatten b;
(1,2,3,4,5,6,7,8,9,10)
$ cut(a, 0 2 7);
([1,2],[3,4,5,6,7],[8,9,10])
$ cut(a, 2 7);
([3,4,5,6,7],[8,9,10])
$ m=matrix(1 5 9, 12 20 23, 25 29 32)
$ cut(m,2)
(#0 #1
-- --
1 12
5 20
9 23
,#0
--
25
29
32
)
$ cut(m, 1 2)
(#0
--
12
20
23
,#0
--
25
29
32
)
The cut function can be a convenient tool in time-series data analysis. In the example below, we use the cut function to calculate an aggregate measure between two events.
$ incomes=table(2016.07.31 - 10..1 as date, rand(100,10) as income);
$ incomes;
date |
income |
---|---|
2016.07.21 |
78 |
2016.07.22 |
61 |
2016.07.23 |
79 |
2016.07.24 |
15 |
2016.07.25 |
78 |
2016.07.26 |
22 |
2016.07.27 |
30 |
2016.07.28 |
81 |
2016.07.29 |
17 |
2016.07.30 |
52 |
$ eventdates = [2016.07.22, 2016.07.25, 2016.07.29];
$ x = incomes.date.binsrch(eventdates);
$ x;
[1,4,8]
$ incomes.date.cut(x);
([2016.07.22,2016.07.23,2016.07.24],[2016.07.25,2016.07.26,2016.07.27,2016.07.28],[2016.07.29,2016.07.30])
$ table(eventdates as startDate, each(last,incomes.date.cut(x)) as endDate, each(sum,incomes.income.cut(x)) as incomeSum);
startDate |
endDate |
incomeSum |
---|---|---|
2016.07.22 |
2016.07.24 |
155 |
2016.07.25 |
2016.07.28 |
211 |
2016.07.29 |
2016.07.30 |
69 |