help-octave
[Top][All Lists]
Advanced

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

Re: Vectorizing a summation


From: Bård Skaflestad
Subject: Re: Vectorizing a summation
Date: Tue, 29 Jan 2013 16:11:01 +0100

On Tue, 2013-01-29 at 06:48 -0800, rdelgado wrote:
> The actual sum is:
> 
> result = SUM(from i=1...MxN) SUM (from j=1...MxN) (Pi-Pj) * (Xi - Yi) * (Xj
> - Yj)

Aha, that changes things.  The following probably won't reproduce the
exact results due to finite accuracy of floating-point calculations, but
it may nevertheless be sufficient for your needs.

        x = reshape(X, [], 1);
        y = reshape(Y, [], 1);
        p = reshape(P, [], 1);
        
        v = p.*sum(x - y) - sum(p .* (x - y));
        
        result = sum(v .* (x - y));


Sincerely,
-- 
Bård Skaflestad <address@hidden>



reply via email to

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