From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Replacing for loops
Date: Tue, 14 Jun 2011 15:50:21 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 16
Message-ID: <it7vvt$es8$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1308066621 15240 (14 Jun 2011 15:50:21 GMT)
NNTP-Posting-Date: Tue, 14 Jun 2011 15:50:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2906241
Xref: comp.soft-sys.matlab:731886

I have been attempting to write a programme to run through all possible combinations of elements from two lists and add the results of a calculation on each pair together. the following is my current code, which works:

function ans = cor(train1, train2, tau)
ans = 0;
length1 = numel(train1);
length2 = numel(train2);
for n=1:length1
    for m= 1:length2
        ans = ans + exp(-abs(train1(n) - train2(m))/tau);

tau is a scalar, while train1 and train2 are vectors of variable length. Can anyone give me a tip as to how to replace the for loops with something that will work faster?

I'm quite new to MATLAB, so I apologise if it's something trivially easy that I've missed.