How do I reduce the run time of this code?

t=cputime;
pdvect=10.^[-10:0.1:-6];
pgvect=10.^[-10:0.1:-6];
%Variables
psingws=zeros(length(pdvect),length(pgvect));
psingnows=zeros(length(pdvect),length(pgvect));
pmorews=zeros(length(pdvect),length(pgvect));
pmorenows=zeros(length(pdvect),length(pgvect));
pnothing=zeros(length(pdvect),length(pgvect));
psingaws=zeros(length(pdvect),length(pgvect));
psingnoaws=zeros(length(pdvect),length(pgvect));
pmoreaws=zeros(length(pdvect),length(pgvect));
pmorenoaws=zeros(length(pdvect),length(pgvect));
pnothingaws=zeros(length(pdvect),length(pgvect));
pdmat=zeros(length(pdvect),length(pgvect));
pgmat=zeros(length(pdvect),length(pgvect));
%Calculating Ratio
for i1=1:length(pdvect);
pd=pdvect(i1);
i1/length(pdvect)
for j1=1:length(pgvect);
pg=pgvect(j1);
[psingws(i1,j1),psingnows(i1,j1),pmorews(i1,j1),pmorenows(i1,j1),pnothingws(i1,j1)]=wsp_v4([pd pd pd pd pd pd pd pd pd pd pd pd pd pd],[pg pg pg pg pg pg pg pg pg pg pg pg pg pg]);
[psingaws(i1,j1),psingnoaws(i1,j1),pmoreaws(i1,j1),pmorenoaws(i1,j1),pnothingaws(i1,j1)]=aws_v4([pd pd pd pd pd],[pg pg pg pg pg]);
pdmat(i1,j1)=pd;
pgmat(i1,j1)=pg;
end
end
%Ratio of all mutations giving a WS(single and multiple mutation events)[wsp vs. aws]
ratio1=(psingws+pmorews)./(psingaws+pmoreaws);
logratio1=log2(ratio1);
%Ratio of only single mutations giving a WS [wsp vs. aws]
ratio2=psingws./psingaws;
logratio2=log2(ratio2);
%Lower limit
close all
surf(pdvect,pgvect,(psingws)./(psingaws+pmoreaws))
set(gca,'xScale','log','yScale','log');
%Upper limit
figure
surf(pdvect,pgvect,(psingws+pmorews)./(psingaws))
set(gca,'xScale','log','yScale','log');
%Surface of ratio of all mutations giving a WS(single and multiple mutation
%events)[wsp vs. aws]
figure
surf(pdvect,pgvect,logratio1)
set(gca,'xScale','log','yScale','log');
%Surface of ratio of only single mutations giving a WS [wsp vs. aws]
figure
surf(pdvect,pgvect,logratio2)
set(gca,'xScale','log','yScale','log');
e=cputime-t;

Answers (1)

Have you used the profiler to identify the bottlenecks?
One small thing is that you calculate the following many times when you could calculate them once and store them:
length(pdvect)
length(pgvect)
[pd pd pd pd pd pd pd pd pd pd pd pd pd pd]
%etc.
Basically, anything that does not change inside of the for-loop should not be calculated inside of it.
What does this line do?
i1/length(pdvect)

Categories

Find more on MATLAB Parallel Server in Help Center and File Exchange

Tags

Asked:

on 12 Sep 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!