KDB / Q Resetting random elements on newly created list (numerical vs symbol/char) -
i having random (y) list (x) elements (seed elements in 2nd dimension) want reset values on random factor (z) between 0f , 1f. numerical lists code works fine
q)seed:20 q){(0>z-(x#seed)?\:1f)*(x#seed)?\:y}[3;10;0.25]
how can extend code running in case of symbols (or char lists) that
q){(0>z-(x#seed)?\:1f)*(x#seed)?\:y}[3;`8;0.25] q){(0>z-(x#seed)?\:1f)*(x#seed)?\:y}[3;" ";0.25]
i guess multiplication in middle problem, dont know how make more generic.
maybe there more elegant solution problem creating 2 lists , handling that.
thanks
to make generic matter of using booleans index rather using them multiply (as are). however, non numerical lists you'd need reset values nulls rather zero. these nulls have correct null corresponding type of input (i.e. 0ni ints, 0nj longs, ` symbols, " " chars etc) - can achieved 'first 0#'
so generic version of function like:
q){seed cut @[(x*seed)?y;where 0<=z-(x*seed)?1f;:;first 0#y]}[3;10;0.25] 9 3 9 4 9 9 8 7 6 1 3 4 9 3 9 2 6 5 3 5 6 0 8 9 0 8 9 1 5 7 4 3 2 3 7 6 9 8 2 2 8 9 8 2 5 1 2 1 3 q){seed cut @[(x*seed)?y;where 0<=z-(x*seed)?1f;:;first 0#y]}[3;`8;0.25] eklinmcm ikfknpam pjncfmob mmhpkfap bhakgffh khkag.. neohaicn fiajkigo bllnecdn hpnommjb pkhpildh lacif.. feinbhmg mbpkjapc dgippbmi lelmkfoe .. q){seed cut @[(x*seed)?y;where 0<=z-(x*seed)?1f;:;first 0#y]}[3;" ";0.25] "rk nts d cxfkp " "oiusc udpliqbqnzapql" "qhk yauhroflprr lwuw"
having said that, may not best way approach initial problem - suggestion making generic.
ps. if still want zeros in numerical lists use zero-fill
q)0^{seed cut @[(x*seed)?y;where 0<=z-(x*seed)?1f;:;first 0#y]}[3;10;0.25] 7 0 5 1 1 0 3 2 0 0 0 0 0 3 0 9 7 9 7 7 6 9 4 0 4 4 0 6 2 9 5 1 0 0 8 9 6 3 4 0 5 0 8 1 1 6 4 9 0 1 9 5 8 9 0 3 9 0 0 1
Comments
Post a Comment