kdb+

enumerating contiguous ones in kdb+


Given a list of 0s and 1s, say l: 0 0 1 1 1 0 0 1 1 I would like to get a sequence beginning with 1 for each contiguous set of 1s. Thus the required output should be l: 0 0 1 2 3 0 0 1 2. How can I achieve this in kdb+?


Solution

  • You can use scan:

    q){y*x+y}scan 0 0 1 1 1 0 0 1 1
    0 0 1 2 3 0 0 1 2