help-octave
[Top][All Lists]
Advanced

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

Re: Iterative summation using sum(), fails with ezmesh


From: Jordi Gutiérrez Hermoso
Subject: Re: Iterative summation using sum(), fails with ezmesh
Date: Thu, 10 Apr 2014 22:16:20 -0400

On Thu, 2014-04-10 at 22:54 +0200, Erik Leunissen wrote:

>                    4
>                   __
>     C(x,t) = 2*x* \  - exp(x+(i-0.5))^2
>                   /   -----------------
>                   --        t
>                  i=-4
> 
> Of course I'd prefer a vectorized way,

I assume the negative goes inside the exp, not outside, as that is
what you had in your code earlier.

That being said, I can see a couple of ways. I have not timed either
of them. Take your pick. There's a very straightforward way with
arrayfun and a less transparent way using permute and exploiting
broadcasting:

    function z = C (xx, tt)
      z = arrayfun ( @(x, t) 2*x*sum (exp ( -(x + (-4:4) - 0.5).^2))/t,
                    xx, tt)
      z = 2*xx.*sum (exp ( -(xx + permute (-4:4, [1,3,2]) - 0.5).^2), 3)./tt
    endfunction

HTH,
- Jordi G. H.





reply via email to

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