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$>
References: <gqdj93$qa4$> <gqdn0i$a5r$> <gqdr01$jp4$> <gqduat$hug$> <gqdvgd$bvg$> <gqe1nl$cra$> <gqe4fp$1ml$> <gqe6ja$4fg$> <gqe7j5$evd$> <gqe7ud$9vm$> <gqeh2t$aqc$> <gqf9q6$gad$> <gqg8go$o5j$> <gqgg4e$98j$> <gqghjs$mav$> <gqgj7p$dno$> <gqgjup$48o$> <gqgkiu$il3$> <gqgm9o$jif$> <gqgmkh$cj5$> <gqgoo2$55u$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1238127541 4555 (27 Mar 2009 04:19:01 GMT)
NNTP-Posting-Date: Fri, 27 Mar 2009 04:19:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: 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);

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);

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.