cut

Syntax

cut(X, size|cutPositions)

Arguments

X is a vector.

size is an integer scalar and must satisfy 2<=a<size(X). Function cut divides X into a list of vectors with the given size.

cutPositions is a sorted integer vector. Function cut divides X into a list of vectors at the specified positions. Each of the vectors in the result starts at a position that is specified in cutPositions.

Details

A vector is divided into a list of vectors. Please 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])

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