扫描运算符如何帮助识别规则适用于连续 2 个观测值的索引?
How does the scan operator help to identify indices where a rule holds for 2 observations in a row?
已根据某些规则创建了一个布尔向量,我们需要确定该规则适用于连续 2 个观察值的索引。下面的代码就是这样做的
indices:0101001101b
runs:{0 x\x}"f"$;
where 2=runs indices
能否请您帮助我了解在 runs 函数的定义中如何使用扫描运算符?感谢您的帮助。
它使用这个特殊的 shorthand 计算指数移动平均线常用:https://code.kx.com/q/ref/accumulators/#alternative-syntax
所以{0 x\x}
等同于:
q){z+x*y}\[0;indices;indices]
0 1 0 1 0 0 1 2 0 1
这实际上是将布尔值用作滚动总和的 on/off 开关(通过布尔值乘法)。它添加 (z+
) 直到它遇到负布尔值,在这种情况下,滚动总和将重置为零。
英文:nextValue
+ [currentValue (starting at 0)
* nextValue
]
当nextValue
为1
时,加1。当 nextValue
为 0
时,结果为零(重置滚动总和)。
像这样的东西可以达到同样的目的,虽然一目了然地阅读起来同样容易(并且使用两次扫描而不是一次):
q){s-maxs not[x]*s:sums x}indices
0 1 0 1 0 0 1 2 0 1i
Terry 已经回答了您关于runs
工作原理的问题。
比较相邻的项目很常见。您可能更喜欢使用 prior
关键字。当然更容易看到它在做什么。
q)where (and) prior indices
,7
已根据某些规则创建了一个布尔向量,我们需要确定该规则适用于连续 2 个观察值的索引。下面的代码就是这样做的
indices:0101001101b
runs:{0 x\x}"f"$;
where 2=runs indices
能否请您帮助我了解在 runs 函数的定义中如何使用扫描运算符?感谢您的帮助。
它使用这个特殊的 shorthand 计算指数移动平均线常用:https://code.kx.com/q/ref/accumulators/#alternative-syntax
所以{0 x\x}
等同于:
q){z+x*y}\[0;indices;indices]
0 1 0 1 0 0 1 2 0 1
这实际上是将布尔值用作滚动总和的 on/off 开关(通过布尔值乘法)。它添加 (z+
) 直到它遇到负布尔值,在这种情况下,滚动总和将重置为零。
英文:nextValue
+ [currentValue (starting at 0)
* nextValue
]
当nextValue
为1
时,加1。当 nextValue
为 0
时,结果为零(重置滚动总和)。
像这样的东西可以达到同样的目的,虽然一目了然地阅读起来同样容易(并且使用两次扫描而不是一次):
q){s-maxs not[x]*s:sums x}indices
0 1 0 1 0 0 1 2 0 1i
Terry 已经回答了您关于runs
工作原理的问题。
比较相邻的项目很常见。您可能更喜欢使用 prior
关键字。当然更容易看到它在做什么。
q)where (and) prior indices
,7