You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Q语言中rescaledUnits函数报'length'错误的修复咨询

修复Q语言重缩放函数的错误

错误原因

  1. 拼写错误:代码中定义的中间列是ones:1,但后续update语句中误写为one,导致引用不存在的列。
  2. 长度不匹配:在带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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 16:12:32