help-octave
[Top][All Lists]
Advanced

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

Re: Efficient code for operating on pairwise vector diffs


From: Joseph Wakeling
Subject: Re: Efficient code for operating on pairwise vector diffs
Date: Mon, 02 Nov 2009 15:54:21 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

Jaroslav Hajek wrote:
> If X is a DxN matrix, you can get a dxNxN array of pairwise
> differences using, for instance using spread indexing
> 
> D = X(:,:,ones (1, N)) - reshape (X, d, 1, N)(:,ones (1, N), :);
> 
> or using bsxfun
> 
> D = bsxfun (@minus, X, reshape (X, d, 1, N));
> 
> the relative efficiency will depend on your version of Octave (bsxfun
> wins if you use the bleeding edge sources).

That's brilliant -- thank you very much!  Seems to provide exactly what
I need.  I will play and see where this goes.

> converts this to an NxN cell array of dx1 vectors. Note that unless
> your mapper function is fast enough (built-in?), this isn't going to
> win you much, because your function will still be called NxN times.

I'm hoping it will win a fair amount relative to the code I inherited --
NxN is an inevitability, but I think the actual implementation was
surely less efficient than that ...

> If F is a fixed mapping and relatively simple, there may be a better
> way to do the trick in a few calls. See also
> http://octave.sourceforge.net/doc/f/pdist.html

The code has to be both MATLAB- and Octave-compatible, so this is
probably out unless we copy from octave-forge.  Probably simpler to
stick with functions that are built-in with both programs.

> See the above comment. Note that Octave also has a C++ extension as an option.

Sure -- but it's preferable to remove techy problems like compiling
extensions from users' lives. :-)

> Code is always better if you want good answers. You can only extract a
> small (but working!) skeleton. Unless your co-workers are idiots, they
> won't bark at you for showing a few lines, especially if you're trying
> to improve your common work.

_I_ know that, but some people (and some fields:-) are paranoid about
such things.  I'll have a word with people and see what we can do.  I'm
trying to persuade them to accept a preprint release of the article; if
that goes ahead maybe we could at the same time present the code for
review to the Octave user community, and we could add a little note of
acknowledgement in the final published paper.  I would certainly like to
see the code end up in octave-forge at some point.

Thanks again for your detailed and helpful remarks,

    -- Joe


reply via email to

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