Manipulating the elements in the table

Hi everyone, 

I am really new to the KDB, and I stuck at this exercise for a while… can’t figure it out how to solve it, all I know is that I might be need to use scan and $ to solve this issue …

The logic is below,

  1. a-m>0 , b=a-m , e=m

  2. a-m=0 , e=m

  3. a-m<0 , if (1) b>0         (a) ((a+b)-m)<0 , e=a+b , b=0

                                         (b) ((a+b)-m)>0 , e=m , b=a+b-m

                    (2) b<0 , e=a

and the desired result is below

I will be grateful if someone can enlighten me on this issue …

Thank you all.

Jimmy

as you indicate, you can use the scalar conditional $ and scan to pass the previous value of b (and e) to the next call of the function.

/ first build the table
sym:raze 10#/:JPMGE
time:12:00+til 20
a:100*1 3 1 2 4 8 1 1 1 3 1 2 4 4 1 1 2 1 1 1
m:100*1 1 6 3 1 1 2 6 5 3 1 1 1 1 8 1 1 1 2 1
t:(sym;time;a;m)

/ declare function following your rules

f:{[be;a;m]b:first be;$[0<am:a-m;(am;m);0=am;(b;m);b<0;(b;a);m>ab:a+b;(0;ab);(ab-m;m)]}

/ scan over rows to see the values of b and e

update be:f[0;a;m] from t

/ scan over rows only taking e

update e:f[0;a;m][;1] from t

Thank you Nick, 

After reading your code … one thing I am certainly sure, I am really new, especially the syntax…

But thanks for enlightening me on this issue, really appreciate.

Best,

Jimmy  

Nick? 2016?2?26??? UTC??4?31?01???

as you indicate, you can use the scalar conditional $ and scan to pass the previous value of b (and e) to the next call of the function.

/ first build the table
sym:raze 10#/:JPMGE
time:12:00+til 20
a:100*1 3 1 2 4 8 1 1 1 3 1 2 4 4 1 1 2 1 1 1
m:100*1 1 6 3 1 1 2 6 5 3 1 1 1 1 8 1 1 1 2 1
t:(sym;time;a;m)

/ declare function following your rules

f:{[be;a;m]b:first be;$[0<am:a-m;(am;m);0=am;(b;m);b<0;(b;a);m>ab:a+b;(0;ab);(ab-m;m)]}

/ scan over rows to see the values of b and e

update be:f[0;a;m] from t

/ scan over rows only taking e

update e:f[0;a;m][;1] from t