comp.soft-sys.matlab
[Top] [All Lists]

Re: How to Speed Up Matlab Code

Subject: Re: How to Speed Up Matlab Code
From: Sam
Date: Fri, 30 Sep 2005 10:00:08 -0400
Newsgroups: comp.soft-sys.matlab
Jos wrote:
>
>
> Sam wrote:
>>
>>
>> As I am a relatively new user in Matlab, I would like some help
> in
>> speeding up
>> my Matlab code. I think my problem is due to the loops in my
>> program as
>> it requires a significant amount of time to run it. Is there
>> someone
>> who knows how to speed up the code below? I hope vectorizing is
> an
>> option, as the code below is painfully slow. Anyone who has any
>> advice,
>> all help is welcome.
>>
>>
>> %The matrices in the code below can be accurately described as
>> follows:
>> %full_data: a 250,000x10 matrix (already exists)
>>
>> %data250_probs: a 250,000x374 matrix (already exists)
>>
>> % dDdP: Will be a (374x374) symmetric square matrix containing
> the
>> column sum of the dot product of Pi.*(1-Pi).*dVijdp.*q on the
>> diagonal
>> and Pi.*Pj.*dVijdp.*q on the off diagonal. (to be generated by
> the
>> code
>> below based on the contents of the already existing matrices.
>>
>> dDdP=zeros(374,374);
>> q=full_data250(:,3);
>> for i = 1:374
>> Pi=data250_probs(:,i);
>> for j = i:374
>> Pj=data250_probs(:,j);
>> dVijdp=(-.00122 ...
>> + full_data250(:,7).*-.0000261207 ...
>> + full_data250(:,8).*-.000156581 ...
>> + full_data250(:,9).*-.000121213 ...
>> + full_data250(:,10).*.00000564124);
>> if i==j
>> dDdP(i,j)=sum(Pi.*(1-Pj).*dVijdp,1);
>> else
>> dDdP(i,j)=-sum(Pi.*Pj.*dVijdp,1);
>> dDdP(j,i)=dDdP(i,j);
>>
>> end
>> end
>> end
>>
>>
>
> A couple of suggestions:
> 1. pre-allocate dDdP (zeros(374)
> 2. Take the calculation of dVijdp out of the loop as it seems to be
> always the same
> 3. Remove the if statement: let j run from (i+1):374, and add a
> second loop (for i=1:374) in which you calculate the diagonal
>
> hth
> Jos

Thanks. That speeds things up a lot.
-Sam

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy