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 19:31:23 +0000

Sorry about the top post.  I don't have physical access to my normal e-mail 
client right now.

I won't try to defend my use of RESHAPE over the

    d = (X-Y)(:)

statement.  Indexing into intermediate results certainly is useful.  I 
originally started using RESHAPE because the '(:)' method, at least at one 
point, would generate an entirely new vector (including data copying) while 
RESHAPE would "just" reset some internal size variable on the already existing 
object.  Maybe I'm just misremembering...

Broadcasting for pairwise differences is wonderful.  That said, I *usually* 
prefer spelling out the equivalent BSXFUN call due to greater expression of 
intent (at least to my eyes) when I come back to maintain the code in six 
months, but that may just be lack of experience with the broadcasting feature.

Finally, I'd like to add one word of caution with respect to the original 
question.  Unless the images are very small (e.g., smaller than about 100x100 
pixels), the size of the intermediate matrix may reduce the utility of the 
fully vectorised approach.  Images of size 100x100 obviously contain 10,000 
points whence the (dense) difference matrix 'E' becomes size 10,000-by-10,000 
(i.e. 100 million entries).  Using type DOUBLE then means that we need 800 MB 
of memory just to store the matrix.  This may be a case for which a partially 
vectorised loop might be advantageous due to reduced memory pressure.


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

________________________________________
From: address@hidden address@hidden on behalf of Jordi Gutiérrez Hermoso 
address@hidden
Sent: 29 January 2013 18:17
To: Bård Skaflestad
Cc: rdelgado; address@hidden
Subject: Re: Vectorizing a summation

On 29 January 2013 10:56, Bård Skaflestad <address@hidden> wrote:
>         d = reshape(X - Y, [], 1);

Just want to say, I find

    d = (X-Y)(:)

more idiomatic.

Also, don't forget something like

    y = magic(5)(1,:)

    y - y'

i.e. using broadcasting for computing pairwise differences.

- Jordi G. H.


reply via email to

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