Path: news.mathworks.com!not-for-mail
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$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1308066621 15240 172.30.248.35 (14 Jun 2011 15:50:21 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 14 Jun 2011 15:50:21 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2906241
Xref: news.mathworks.com 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);
    end
end
end

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.