Q语言中rescaledUnits函数报'length'错误的修复咨询
修复Q语言重缩放函数的错误
错误原因
- 拼写错误:代码中定义的中间列是
ones:1,但后续update语句中误写为one,导致引用不存在的列。 - 长度不匹配:在带
where条件的update中,使用全表列ones和a赋值,而where筛选后的行数量与全表列长度不一致,触发'length错误。Q语言要求where子句中赋值的右侧必须是与筛选后行数相同的向量,或是可自动广播的原子值。
修复后的函数(简洁版)
直接通过向量条件运算生成目标列,无需多余中间列:
rescaledUnits:{[t] update uc:?[x>=y; 1; ceiling max(x;y)%min(x;y)], up:?[x>=y; ceiling max(x;y)%min(x;y); 1] from t };
修复后的函数(保留中间列版)
修正拼写错误,并使用原子值替代全表列,避免长度问题:
rescaledUnits:{[t] t:update l:max(x;y), s:min(x;y) from t; t:update a:ceiling l%s from t; t:update uc:1, up:a where x>=y from t; t:update uc:a, up:1 where x<y from t; t };
验证结果
调用rescaledUnits[t]会得到符合需求的结果:
x y l s a uc up ----------------------- 1 5.1 5.1 1 6 6 1 2 2.4 2.4 2 2 2 1 3 3.3 3.3 3 2 2 1 4 4.5 4.5 4 2 2 1 5 1.5 5 1.5 4 1 4
内容的提问来源于stack exchange,提问作者marital_weeping
相关产品推荐
相关产品推荐

