kdb

trouble with rescaling function


I have a table t

t:([] x: 1 2 3 4 5; y: 5.1 2.4 3.3 4.5 1.5);
x y  
-----
1 5.1
2 2.4
3 3.3
4 4.5
5 1.5

and I'm trying to rescale the x and y rows of t such that the larger one becomes 1 and the other takes the value ceiling max(x;y)%min(x;y) as:

rescaledUnits:{[t]
    t:update l:max(x;y), s:min(x;y) from t;
    t:update a:ceiling l%s, ones:1 from t;
    t:update uc:ones, up:a where x>=y from t;
    t:update uc:a, up:ones where x<y from t;
    t
 };

but on calling it I get

show rescaledUnits[t]

q)

'length
    t:update a:ceiling l%s, one:1 from t;
    t:update uc:one, up:a where x>=y from t;
      ^
    t:update uc:a, up:one where x<y from t;

How do i fix the above function?


Solution

  • Your update statements are wrong

    q)t:([] x: 1 2 3 4 5; y: 5.1 2.4 3.3 4.5 1.5)
    q)t
    x y
    -----
    1 5.1
    2 2.4
    3 3.3
    4 4.5
    5 1.5
    q)rescaledUnits
    {[t]
        t:update l:max(x;y), s:min(x;y) from t;
        t:update a:ceiling l%s, ones:1 from t;
        t:update uc:ones, up:a from t where x>=y ;
        t:update uc:a, up:ones from t where x<y ;
        t
     }
    q)rescaledUnits[t]
    x y   l   s   a ones uc up
    --------------------------
    1 5.1 5.1 1   6 1    6  1
    2 2.4 2.4 2   2 1    2  1
    3 3.3 3.3 3   2 1    2  1
    4 4.5 4.5 4   2 1    2  1
    5 1.5 5   1.5 4 1    1  4
    q)
    

    The where clause comes at the end. Q-SQL queries have the form of select <cols> <by> from T<exp> <where> You can read about it here https://code.kx.com/q4m3/9_Queries_q-sql/