Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: avoiding loops to built a z matrix of the most efficient form
Date: Fri, 27 Mar 2009 04:19:01 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 31
Message-ID: <gqhk3l$4eb$1@fred.mathworks.com>
References: <gqdj93$qa4$1@fred.mathworks.com> <gqdn0i$a5r$1@fred.mathworks.com> <gqdr01$jp4$1@fred.mathworks.com> <gqduat$hug$1@fred.mathworks.com> <gqdvgd$bvg$1@fred.mathworks.com> <gqe1nl$cra$1@fred.mathworks.com> <gqe4fp$1ml$1@fred.mathworks.com> <gqe6ja$4fg$1@fred.mathworks.com> <gqe7j5$evd$1@fred.mathworks.com> <gqe7ud$9vm$1@fred.mathworks.com> <gqeh2t$aqc$1@fred.mathworks.com> <gqf9q6$gad$1@fred.mathworks.com> <gqg8go$o5j$1@fred.mathworks.com> <gqgg4e$98j$1@fred.mathworks.com> <gqghjs$mav$1@fred.mathworks.com> <gqgj7p$dno$1@fred.mathworks.com> <gqgjup$48o$1@fred.mathworks.com> <gqgkiu$il3$1@fred.mathworks.com> <gqgm9o$jif$1@fred.mathworks.com> <gqgmkh$cj5$1@fred.mathworks.com> <gqgoo2$55u$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1238127541 4555 172.30.248.37 (27 Mar 2009 04:19:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 27 Mar 2009 04:19:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:528093

Well, you can shave off about 10% of the run-time by using the following:



nrv = 3000; % random variable
nrp = 5000; % number of 2D-points 
v1 = rand(nrp,2);
v2 = rand(nrv,2);
ia = [1 2; 3 4];
determ = 2;
d = 2;
% There is no reason to keep calculating these guys in the loop!
fact1 = 1/((2*pi)^(0.5*d)*(determ)^(0.5));
fact2 = log(fact1);

tic
lpms = zeros(1,nrv);
for jj = 1:nrv
    t1 = v1(:,1) - v2(jj,1);    
    t2 = v1(:,2) - v2(jj,2);
    zm = -0.5*(t1.^2*ia(1) + (ia(2)+ia(3))*t1.*t2 + ia(4)*t2.^2);
    pm = fact1 * exp(zm);
    lpm = fact2 + zm;
    lpms(jj) = sum(lpm);
end
toc


The profiler says that the exp function takes 70% of run-time.  This being the case, I would be surprised if there was a method that was WAY faster out there.  Of course Bruno (and others!) has surprised me before, but there have been several threads in the past dealing with trying to speed up exp.  This being the case, short of a faster approximating scheme to exp, I think this may be close to bottomed out.

Good luck.