Still unable to understand this kind of functions

Hello, I need a function to compute a new column ADX from a table t

t:(A:1 2 3 4 5 6 7 8 9;DX:5.8 6.5 0.1 7.4 2.2 4.4 5.2 6.0 0.5;C:defrdtac`b)

I’d like to have ADX as 3 functions:

f1: null or 0 applied to first 3 rows 

f2: 3 mavg DX applied to row 4

f3: DX + prev ADX applied to remaining rows 

How can I do this?

Best regards

Francisco

q)t[`ADX]:(3#0.),sums (3 mavg t.DX)[3],4_t.DX;t

A DX C ADX

----------------

1 5.8 d 0

2 6.5 e 0

3 0.1 f 0

4 7.4 r 4.666667

5 2.2 d 6.866667

6 4.4 t 11.26667

7 5.2 a 16.46667

8 6 c 22.46667

9 0.5 b 22.96667

and optimise the fourth mavg:  (3 mavg 4#t.DX)

Thanks for your help, but I had a mistake in the third funtion:

f3: DX + prev ADX applied to remaining rows

f3: (DX + 2*(prev ADX))%3 applied to remaining rows

Cheers

Francisco

it could be
q)t[`ADX]:(3#0.),x{(y+2*x)%3}':(x:(3 mavg t.DX)[3]),4_t.DX;t

A DX C ADX

----------------

1 5.8 d 0

2 6.5 e 0

3 0.1 f 0

4 7.4 r 4.666667

5 2.2 d 3.022222

6 4.4 t 3.666667

7 5.2 a 4.933333

8 6 c 5.733333

9 0.5 b 2.333333

Using the built-in scan shortcut:

q)s:avg t[`DX]1 2 3;   /seed

q)update ADX:(s,s(2%3)\1_DX%3) from t where i>2

A DX  C ADX


1 5.8 d

2 6.5 e

3 0.1 f

4 7.4 r 4.666667

5 2.2 d 3.844444

6 4.4 t 4.02963

7 5.2 a 4.419753

8 6   c 4.946502

9 0.5 b 3.464335

Terry

Nice one,

I had to look at the reference to find out what 

  (float) (float) 

did.

http://code.kx.com/wiki/Reference/BackSlash 

(see note “As of kdb+3.1 2013.07.07, scan has a built in function for the following…”)

Thanks a lot!