KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ how to find the first element for each row which satisfy the condition Re: how to find the first element for each row which satisfy the condition

  • Laura

    Administrator
    May 16, 2022 at 12:00 am

     

    q)u 
    isMatched index id 
    ------------------------- 
    1100b 0 1 2 3 a b c d 
    1100b 1 0 2 3 a b c d 
    0011b 0 1 2 3 a b c d 
    
    q)fw:first where@ / composition 
    q)select index:index@'j,id:id@'j from update j:fw each isMatched from u 
    index id 
    -------- 
    0 a 
    1 a 
    2 c

     

    Or you might prefer the same thing as vector operations:

     

    q)u[`index`id]@':fw each u`isMatched 
    0 1 2 
    a a c 
    
    q)ts:10000 select index:index@'j,id:id@'j from update j:fw each isMatched from u 
    65 2832 
    
    q)ts:10000 u[`index`id]@':fw each u`isMatched 
    21 1376

     

    In the vector expression, fw each u`isMatched corresponds to fw each isMatched from u. But instead of forming column j of a new temporary table, it is applied direct to the index and id columns of u. Notice how Each Left : takes Index At Each @' as its argument to derive binary function Index At Each Each Left.

    Your sublists are short; if they were longer you might prefer faster ?'[;1b] to fw each:

     

    q)ts:10000 select index:index@’j,id:id@’j from update j:?'[;1b] isMatched from u

    61 3056

    q)ts:10000 u[`index`id]@’:u[`isMatched]?’

    1b

    16 1568