MATLAB: Consecutive combination of numbers from a list

consecutive combination

any quick methods to obtain a count of complete consecutive n numbers from a array list 1:m e.g. a list 1:21 and n=3 so
it would be 1:3,4:6,7:9,10:12,13:15,16:18,19:21,2:4,5:6,…17:19,3:5,…18:20= 19 combinations.

Best Answer

>> (1:19)'+(0:2) % (1:m-n+1)'+(0:n-1)
ans =
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 10
9 10 11
10 11 12
11 12 13
12 13 14
13 14 15
14 15 16
15 16 17
16 17 18
17 18 19
18 19 20
19 20 21