help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vectorize a function that depends on previous function value


From: Kai Torben Ohlhus
Subject: Re: Vectorize a function that depends on previous function value
Date: Mon, 19 May 2014 19:28:45 +0200

On Mon, May 19, 2014 at 4:43 PM, Juan Pablo Carbajal <address@hidden> wrote:
On Mon, May 19, 2014 at 3:52 PM, gergo <address@hidden> wrote:
>
> a = ones(10,1);
> a(2:end) = a(1:end-1) *2;
>
> This yields a = [1 2 2 2 ...]';
> What I needed was: a = [1 2 4 8 ...]';
>
> Any idea?
>
> Thanks
>
> Gerald
>

I do not think you can vectorize a time dependence relation easily,
just try to vectorize
a(n) = a(n-1)+f(a(n-1));
This is exactly what integrators like lsode and ode45 do. What you can
do if your code is slow is to pass
that loop to C/C++ code.
https://www.gnu.org/software/octave/doc/interpreter/Getting-Started-with-Oct_002dFiles.html

You can also try these tricks
https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html
Specially "Function Application" and "Accumulation"

_______________________________________________
Help-octave mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-octave

Sorry I also run out of ideas for this tough data dependency issue.

Best,
Kai

reply via email to

[Prev in Thread] Current Thread [Next in Thread]