On Mon, Nov 16, 2009 at 9:29 AM, Richard Kubina <rjkub...@oakland.edu> wrote:
> Hi, first time poster, new J user.
> I cannot figure out how to do a certain sorta algorithm in J without a for
> loop.

Of course, J has for loops for a reason.

> I have an array of data:
> a1=: 10 7 8 3 5
> and the other to be calculated iteratively, except for the first value which
> is already known.
> a2=: 6
>
> So I need to take the 6 and pair it with 10, do some calculations append the
> new number to the end of a2.
> I used this function to do this
>
> GetEWMAData =: monad : 'for_x. }.SubGroupedData do. EWMAData=:EWMAData,
> (lambda*x)+((1-lambda)*({:EWMAData)) end.'
>
> where lambda is 0.2, SubGroupedData is a1 here and EWMAData is a2, getting
> updated by appending the new number to the end of the list.
However, I think this would be simpler to read if it was a dyad which
used x and y for its arguments:

GetEWMAData=: adverb define
:
  for_Y. }.y do.
    x=.x, (m*Y) + (1-m)*{:x
  end.
)

   a2 0.2 GetEWMAData a1
6 6.2 6.56 5.848 5.6784

However, I when I was writing this, Matthew Brand sent a message
which looks relevant.  So I will stop here, for now.

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

Reply via email to