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
-
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 tofw each isMatched from u
. But instead of forming columnj
of a new temporary table, it is applied direct to theindex
andid
columns ofu
. 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]
tofw 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