I see that several functions that I need have similar pattern:
Let x - boolean vector (result of some predicate)
and y - vector of some data, where #x = #y
then result of function f is
f(0) = y(0)
f(i) = if x(i) = 1 then y(i) else f(i-1)

so I wrote function c1p0 (stands for "copy for 1 previous for 0")
c1p0 =: (-~i.@:#) @: ; @: (< @: i. @: # ;. 1)@[  { ]

with it if I have predicate function p and data function g I can use it as
(p c1p0 g) data

it can be used for closest local minimum and maximum too
locmin =: (1, }.<}:) c1p0 ]
locmax =: (1, }.>}:) c1p0 ]

It perform not as well as your suggestions but it can be used for other cases.
Now I wonder if it can be improved in terms of time?


--
Mikl
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to