help-octave
[Top][All Lists]
Advanced

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

Efficient code for operating on pairwise vector diffs


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

Hello all,

Suppose that I have a set of N d-dimensional vectors, which I guess
could be represented as either a matrix,

   X = [X1 X2 ... XN]

or a cell:

   X = {X1 X2 ... XN}

I'm trying to implement a function which consists of the following steps:

  (i) calculate the pairwise vector differences Dij = Xi - Xj

  (ii) perform some function on each of the vector differences,
       Fij = F(Dij)

  (iii) Construct the averages aveFi = mean of Fij calculated over
        the subscript j.

My guess is that the best way to do this is to represent the vector
differences in a NxN cell whose (i,j)'th element is the d-dimensional
vector X(:,i)-X(:,j).  Then I can call cellfun to apply the function F
and finally the averages can be achieved by some use of mean(cell2mat())
or whatever.

Now, the challenge is how to generate the vector differences
efficiently.  I can do,

for i=1:N
    for j=1:N
        D{i,j} = X(:,i) - X(:,j)
    end
end

... but that seems very inefficient, which is exactly what I'm trying to
avoid(*).  Can anyone advise an efficient way to carry out the above?

Thanks & best wishes,

    -- Joe

(*) Motivation for this whole query: I'm editing some MATLAB code by a
colleague which we're going to release some time soon(**).  His code
works but is not very efficiently written from a MATLAB/Octave point of
view, and so he's used C extensions for some parts.

I think that the code can be made efficient without needing to use C,
which would be very nice as it would allow it to be run in Octave as
well as MATLAB.  But my implementation is foundering on implementing
the above code without costly for loops ...

(**) In answer to 'When?' and 'Why can't we see the actual code now?':
it comes down to academic confidentiality.  If it was just my work I
would happily share, but I can't presume on the part of my co-workers.
>From a publication point of view, we've done all the necessary work and
could just chuck the existing code over the wall; but if I can make sure
that the supporting information to our paper contains Octave-compatible
code, I would like to.  Hope you understand.


reply via email to

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