<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Speeding Up MATLAB Applications</title>
	<atom:link href="http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Sarah</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32690</link>
		<dc:creator>Sarah</dc:creator>
		<pubDate>Fri, 18 Nov 2011 13:57:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32690</guid>
		<description>Hi all.

I found a small mistake in the final version, n should be set to 2 instead of 3.  I apologize for the mistake.    

Cheers,
Sarah</description>
		<content:encoded><![CDATA[<p>Hi all.</p>
<p>I found a small mistake in the final version, n should be set to 2 instead of 3.  I apologize for the mistake.    </p>
<p>Cheers,<br />
Sarah</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prasanna</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32446</link>
		<dc:creator>prasanna</dc:creator>
		<pubDate>Sat, 27 Aug 2011 17:06:58 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32446</guid>
		<description>Although I made use of vectorisation in the first code it runs slower than the second code. I would like to know if it is because of 3 dimensional matrices which i am creating in the vectorised code.

FIRST CODE:

&lt;pre&gt;
clc;
clear all;
close all;
tic;
fs = 100;
fftl = 128;
noi = 50;
maxnof = 1000;
snr  = -5:-1:-25;
reqnof = ones(1,size(snr,2));
prevnof = 0;
h= waitbar(0);
noisepeak = zeros(1,noi);
signoipeak = zeros(1,noi);
wi = 0;
step = 1;

for k = 1:size(snr,2)
    disp(snr(k));
for nos = 1:1000
    nof = prevnof + step;
    t = 0:1/fs:(nof*fftl*noi - 1)/fs;
    signal = cos(2*pi*25*t);
    noise = sqrt(0.5/(10^(snr(k)/10)))*randn(1,nof*fftl*noi);
    fftnoise = abs(fft( reshape(noise,fftl,nof,noi) ));
    tempfftnoise = sum(fftnoise,2)/nof;
    noisepeak = fftl*max(tempfftnoise,[],1)./sum(tempfftnoise,1);
    signoi = noise + signal;
    fftsignoi = abs(fft( reshape(signoi,fftl,nof,noi) ));
    tempfftsignoi = sum(fftsignoi,2)/nof;
    signoipeak = fftl*max(tempfftsignoi,[],1)./sum(tempfftsignoi,1);


sort_signoi = sort(reshape(signoipeak,1,noi));
sort_noise = sort(reshape(noisepeak,1,noi));
if sort_signoi(noi*10/100 + 1) &gt; sort_noise(noi*90/100 + 1)
    reqnof(k) = nof;
    prevnof = nof - step;
    wi = 0;
    break;
else
    wi = wi + 1;
    if wi == 1
        step = step + 1;
        wi = 0;
    end
end
end
waitbar(k/size(snr,2),h);
end
semilogy(snr,reqnof,&#039;o&#039;);
close(h);
save farvar;
toc,
&lt;/pre&gt;

SECOND CODE:

&lt;pre&gt;
clc;
clear all;
close all;
tic;
fs = 100;
fftl = 128;
noi = 50;
maxnof = 1000;
snr  = -5:-1:-25;
reqnof = ones(1,size(snr,2));
prevnof = 0;
h= waitbar(0);
noisepeak = zeros(1,noi);
signoipeak = zeros(1,noi);
wi = 0;
step = 1;

for k = 1:size(snr,2)
    disp(snr(k));
for nos = 1:1000
    nof = prevnof + step;
    t = 0:1/fs:(nof*fftl - 1)/fs;
    signal = cos(2*pi*25*t);
for j = 1:noi
    noise = sqrt(0.5/(10^(snr(k)/10)))*randn(1,nof*fftl);
    tempfftnoise = sum((abs(fft( reshape(noise,fftl,nof) )))&#039;)/nof;
    noisepeak(j) = fftl*max(tempfftnoise)/sum(tempfftnoise);
    signoi = noise + signal;
    tempfftsignoi = sum((abs(fft( reshape(signoi,fftl,nof) )))&#039;)/nof;
    signoipeak(j) = fftl*max(tempfftsignoi)/sum(tempfftsignoi);
end

sort_signoi = sort(signoipeak);
sort_noise = sort(noisepeak);
if sort_signoi(noi*10/100 + 1) &gt; sort_noise(noi*90/100 + 1)
    reqnof(k) = nof;
    prevnof = nof - step;
    wi = 0;
    break;
else
    wi = wi + 1;
    if wi == 1
        step = step + 1;
        wi = 0;
    end
end
end
waitbar(k/size(snr,2),h);
end
semilogy(snr,reqnof,&#039;o&#039;);
close(h);
save farvar;
toc,

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Although I made use of vectorisation in the first code it runs slower than the second code. I would like to know if it is because of 3 dimensional matrices which i am creating in the vectorised code.</p>
<p>FIRST CODE:</p>
<pre>
clc;
clear all;
close all;
tic;
fs = 100;
fftl = 128;
noi = 50;
maxnof = 1000;
snr  = -5:-1:-25;
reqnof = ones(1,size(snr,2));
prevnof = 0;
h= waitbar(0);
noisepeak = zeros(1,noi);
signoipeak = zeros(1,noi);
wi = 0;
step = 1;

for k = 1:size(snr,2)
    disp(snr(k));
for nos = 1:1000
    nof = prevnof + step;
    t = 0:1/fs:(nof*fftl*noi - 1)/fs;
    signal = cos(2*pi*25*t);
    noise = sqrt(0.5/(10^(snr(k)/10)))*randn(1,nof*fftl*noi);
    fftnoise = abs(fft( reshape(noise,fftl,nof,noi) ));
    tempfftnoise = sum(fftnoise,2)/nof;
    noisepeak = fftl*max(tempfftnoise,[],1)./sum(tempfftnoise,1);
    signoi = noise + signal;
    fftsignoi = abs(fft( reshape(signoi,fftl,nof,noi) ));
    tempfftsignoi = sum(fftsignoi,2)/nof;
    signoipeak = fftl*max(tempfftsignoi,[],1)./sum(tempfftsignoi,1);

sort_signoi = sort(reshape(signoipeak,1,noi));
sort_noise = sort(reshape(noisepeak,1,noi));
if sort_signoi(noi*10/100 + 1) &gt; sort_noise(noi*90/100 + 1)
    reqnof(k) = nof;
    prevnof = nof - step;
    wi = 0;
    break;
else
    wi = wi + 1;
    if wi == 1
        step = step + 1;
        wi = 0;
    end
end
end
waitbar(k/size(snr,2),h);
end
semilogy(snr,reqnof,'o');
close(h);
save farvar;
toc,
</pre>
<p>SECOND CODE:</p>
<pre>
clc;
clear all;
close all;
tic;
fs = 100;
fftl = 128;
noi = 50;
maxnof = 1000;
snr  = -5:-1:-25;
reqnof = ones(1,size(snr,2));
prevnof = 0;
h= waitbar(0);
noisepeak = zeros(1,noi);
signoipeak = zeros(1,noi);
wi = 0;
step = 1;

for k = 1:size(snr,2)
    disp(snr(k));
for nos = 1:1000
    nof = prevnof + step;
    t = 0:1/fs:(nof*fftl - 1)/fs;
    signal = cos(2*pi*25*t);
for j = 1:noi
    noise = sqrt(0.5/(10^(snr(k)/10)))*randn(1,nof*fftl);
    tempfftnoise = sum((abs(fft( reshape(noise,fftl,nof) )))')/nof;
    noisepeak(j) = fftl*max(tempfftnoise)/sum(tempfftnoise);
    signoi = noise + signal;
    tempfftsignoi = sum((abs(fft( reshape(signoi,fftl,nof) )))')/nof;
    signoipeak(j) = fftl*max(tempfftsignoi)/sum(tempfftsignoi);
end

sort_signoi = sort(signoipeak);
sort_noise = sort(noisepeak);
if sort_signoi(noi*10/100 + 1) &gt; sort_noise(noi*90/100 + 1)
    reqnof(k) = nof;
    prevnof = nof - step;
    wi = 0;
    break;
else
    wi = wi + 1;
    if wi == 1
        step = step + 1;
        wi = 0;
    end
end
end
waitbar(k/size(snr,2),h);
end
semilogy(snr,reqnof,'o');
close(h);
save farvar;
toc,
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sarah Wait Zaranek</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32168</link>
		<dc:creator>Sarah Wait Zaranek</dc:creator>
		<pubDate>Thu, 31 Mar 2011 16:32:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32168</guid>
		<description>Hi Jiang.

I quickly wrote this visualization so we could have something fun to look at!  Here is the code below.  It probably could be more efficent.  


&lt;pre&gt;


function  plotVals( x1,x2,subs,vals )

width = 1+ 1* vals/max(vals);

for i=1:length(vals)
    
    if x1(subs(i,1))&lt;=10 &amp;&amp; x1(subs(i,3))&lt;=10 &amp;&amp; ...
        x2(subs(i,2))&lt;=10 &amp;&amp; x2(subs(i,4))&lt;=10

    plot([x1(subs(i,1)) x1(subs(i,3))],[x2(subs(i,2)) x2(subs(i,4))]...
    ,&#039;-rs&#039;,&#039;LineWidth&#039;,width(i),...
                &#039;MarkerSize&#039;,2, &#039;MarkerEdgeColor&#039;,&#039;b&#039;,...
                &#039;MarkerFaceColor&#039;,&#039;b&#039;);
    hold on
    end
end

hXLabel=xlabel(&#039;x1 values&#039;);
hYLabel=ylabel(&#039;x2 values&#039;);
hTitle= title(&#039;Initial and Final Positions where exponent &lt; gausThresh&#039;) 

set( gca                             , &#039;FontName&#039;   , &#039;Helvetica&#039; );
set([hTitle, hXLabel, hYLabel]       , &#039;FontName&#039;   , &#039;AvantGarde&#039;);
% set([hLegend, gca]                   , &#039;FontSize&#039;   , 8           );
set([hXLabel, hYLabel]               , &#039;FontSize&#039;   , 10          );
set( hTitle                          , &#039;FontSize&#039;   , 12          , ...
                                       &#039;FontWeight&#039; , &#039;bold&#039;      );

&lt;/pre&gt;


Cheers,
Sarah</description>
		<content:encoded><![CDATA[<p>Hi Jiang.</p>
<p>I quickly wrote this visualization so we could have something fun to look at!  Here is the code below.  It probably could be more efficent.  </p>
<pre>

function  plotVals( x1,x2,subs,vals )

width = 1+ 1* vals/max(vals);

for i=1:length(vals)

    if x1(subs(i,1))&lt;=10 &amp;&amp; x1(subs(i,3))&lt;=10 &amp;&amp; ...
        x2(subs(i,2))&lt;=10 &amp;&amp; x2(subs(i,4))&lt;=10

    plot([x1(subs(i,1)) x1(subs(i,3))],[x2(subs(i,2)) x2(subs(i,4))]...
    ,'-rs','LineWidth',width(i),...
                'MarkerSize',2, 'MarkerEdgeColor','b',...
                'MarkerFaceColor','b');
    hold on
    end
end

hXLabel=xlabel('x1 values');
hYLabel=ylabel('x2 values');
hTitle= title('Initial and Final Positions where exponent &lt; gausThresh') 

set( gca                             , 'FontName'   , 'Helvetica' );
set([hTitle, hXLabel, hYLabel]       , 'FontName'   , 'AvantGarde');
% set([hLegend, gca]                   , 'FontSize'   , 8           );
set([hXLabel, hYLabel]               , 'FontSize'   , 10          );
set( hTitle                          , 'FontSize'   , 12          , ...
                                       'FontWeight' , 'bold'      );
</pre>
<p>Cheers,<br />
Sarah</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jiang</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32156</link>
		<dc:creator>Jiang</dc:creator>
		<pubDate>Tue, 29 Mar 2011 05:19:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-32156</guid>
		<description>Loren,
I am not very good at MATLAB and don&#039;t know how to get the &quot;Results.fig&quot;. Would you please give the commands of plotting the figure &quot;Results.fig&quot;?
Thank you.</description>
		<content:encoded><![CDATA[<p>Loren,<br />
I am not very good at MATLAB and don&#8217;t know how to get the &#8220;Results.fig&#8221;. Would you please give the commands of plotting the figure &#8220;Results.fig&#8221;?<br />
Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Crampton Ph.D.</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-31280</link>
		<dc:creator>Robert Crampton Ph.D.</dc:creator>
		<pubDate>Tue, 20 Apr 2010 22:29:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-31280</guid>
		<description>Thank you!</description>
		<content:encoded><![CDATA[<p>Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-31279</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 20 Apr 2010 21:46:46 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-31279</guid>
		<description>Robert-

You preallocated data, but you didn&#039;t take advantage of it.  So you created a large array, and then had it grow even larger when you did

 data=[data spectrum];

the last line in the for-loop.  Since data started large after preallocation, you were asking for successively larger amounts, vs. starting small.  

You should replace that final line with something that indexes into data and replaces the zeros with the right number of values. To do this, you will need to know each time through the loop how many elements you are adding, and where you left off.

data(idx+(1:numThispiece)) = spectrum;

--Loren</description>
		<content:encoded><![CDATA[<p>Robert-</p>
<p>You preallocated data, but you didn&#8217;t take advantage of it.  So you created a large array, and then had it grow even larger when you did</p>
<p> data=[data spectrum];</p>
<p>the last line in the for-loop.  Since data started large after preallocation, you were asking for successively larger amounts, vs. starting small.  </p>
<p>You should replace that final line with something that indexes into data and replaces the zeros with the right number of values. To do this, you will need to know each time through the loop how many elements you are adding, and where you left off.</p>
<p>data(idx+(1:numThispiece)) = spectrum;</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Crampton Ph.D.</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-31278</link>
		<dc:creator>Robert Crampton Ph.D.</dc:creator>
		<pubDate>Tue, 20 Apr 2010 21:39:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-31278</guid>
		<description>I have some code that I inherited at my job that works fine
but gave me the warning:

The variable &#039;data&#039; is growing inside a loop preallocate for speed.

same error for variable &#039;header&#039;


the code is simple and is for getting spectra from a spectrometer output format into Matlab for processing:

the code is included below. Since I am doing many directories with 8000 or more spectra this week I noticed that it slowed down as it progressed. I had never had a speed problem before so I added lines 31-35 to preallocate.
It got considerably slower so I added % before each line to go back to the original and it sped back up. Did I preallocate wrong?



 &lt;pre&gt; 
function [data,nm,headers] = OOcerexin(directory);

%This function reads in data from files in the Cerex spectrometer format.
%It puts the data and wavelength scale into numeric matrices and saves the
%headers in a cell to re-append later.  
%
%Usage: [data,nm,headers]=cerexin(dir)
%where dir can either be the Matlab dir function, or can be a variable
%created using that function (e.g., x=dir;).
%Outputs:
%data = spectral data arranged columnwise
%nm = wavelength vector
%headers = cell array containing headers for each file

%**********************************************************AC 8/2005

data=[];
headers=[];

%read in data files
filenames={directory.name};
[index,j]=listdlg(&#039;Name&#039;,&#039;Select the files to be read in.&#039;,&#039;ListString&#039;,filenames);
drawnow;

%NEED TO PREALLOCATE BEFORE LOOP USE ZEROS COMMAND TEST??
%GET number of spec 
specnum=index(end)-index(1);
%GET  number of pix always 2048 for this instrument
pix=2048;

%preallocate data
%data = zeros(pix,specnum);
%preallocate header
%headers = cell(1,specnum);
%START LOOP
for i=index(1):index(end);
    e=i
    fid=fopen(filenames{i});
    %changed delimiter for OOI
    [stuff]=textscan(fid,&#039;%s %s&#039;,20,&#039;delimiter&#039;,&#039;:&#039;);
    %changed delimiter for spectral data after line 20
    [stuff2]=textscan(fid,&#039;%s %s&#039;,2048);
    fclose(fid);
    
    %save headers % changed 11 to 19 for OOI
    indivheader={[stuff{1,1}(1:20) stuff{1,2}(1:20)]};
    headers=[headers indivheader];
    
    %create data matrix
    % changed 12 to 20 for OOI
    singledata=strvcat(stuff2{2}(1:end));%creates array of strings from cell
    spectrum=str2num(singledata);%converts strings to numbers
    data=[data spectrum];
   
end;

%save wavelength vector
nmcell=stuff2{1,1}(1:end);
nmcat=strvcat(nmcell);
nm=str2num(nmcat);
    
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I have some code that I inherited at my job that works fine<br />
but gave me the warning:</p>
<p>The variable &#8216;data&#8217; is growing inside a loop preallocate for speed.</p>
<p>same error for variable &#8216;header&#8217;</p>
<p>the code is simple and is for getting spectra from a spectrometer output format into Matlab for processing:</p>
<p>the code is included below. Since I am doing many directories with 8000 or more spectra this week I noticed that it slowed down as it progressed. I had never had a speed problem before so I added lines 31-35 to preallocate.<br />
It got considerably slower so I added % before each line to go back to the original and it sped back up. Did I preallocate wrong?</p>
<pre>
function [data,nm,headers] = OOcerexin(directory);

%This function reads in data from files in the Cerex spectrometer format.
%It puts the data and wavelength scale into numeric matrices and saves the
%headers in a cell to re-append later.
%
%Usage: [data,nm,headers]=cerexin(dir)
%where dir can either be the Matlab dir function, or can be a variable
%created using that function (e.g., x=dir;).
%Outputs:
%data = spectral data arranged columnwise
%nm = wavelength vector
%headers = cell array containing headers for each file

%**********************************************************AC 8/2005

data=[];
headers=[];

%read in data files
filenames={directory.name};
[index,j]=listdlg('Name','Select the files to be read in.','ListString',filenames);
drawnow;

%NEED TO PREALLOCATE BEFORE LOOP USE ZEROS COMMAND TEST??
%GET number of spec
specnum=index(end)-index(1);
%GET  number of pix always 2048 for this instrument
pix=2048;

%preallocate data
%data = zeros(pix,specnum);
%preallocate header
%headers = cell(1,specnum);
%START LOOP
for i=index(1):index(end);
    e=i
    fid=fopen(filenames{i});
    %changed delimiter for OOI
    [stuff]=textscan(fid,'%s %s',20,'delimiter',':');
    %changed delimiter for spectral data after line 20
    [stuff2]=textscan(fid,'%s %s',2048);
    fclose(fid);

    %save headers % changed 11 to 19 for OOI
    indivheader={[stuff{1,1}(1:20) stuff{1,2}(1:20)]};
    headers=[headers indivheader];

    %create data matrix
    % changed 12 to 20 for OOI
    singledata=strvcat(stuff2{2}(1:end));%creates array of strings from cell
    spectrum=str2num(singledata);%converts strings to numbers
    data=[data spectrum];

end;

%save wavelength vector
nmcell=stuff2{1,1}(1:end);
nmcat=strvcat(nmcell);
nm=str2num(nmcat);
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: B Cook</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-30540</link>
		<dc:creator>B Cook</dc:creator>
		<pubDate>Tue, 18 Aug 2009 13:40:53 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-30540</guid>
		<description>OK that&#039;s interesting the changes don&#039;t look like much at the moment, I will have to compare side by side to really see the difference I think. My first trial makes me think your laptop runs a bit faster than mine! I made the first change you suggested and it improved to around 1.8s, so not bad I think there shouldn&#039;t  be a big problem to improve on this now.





Thanks for the suggestions!</description>
		<content:encoded><![CDATA[<p>OK that&#8217;s interesting the changes don&#8217;t look like much at the moment, I will have to compare side by side to really see the difference I think. My first trial makes me think your laptop runs a bit faster than mine! I made the first change you suggested and it improved to around 1.8s, so not bad I think there shouldn&#8217;t  be a big problem to improve on this now.</p>
<p>Thanks for the suggestions!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sarah Z</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-30526</link>
		<dc:creator>Sarah Z</dc:creator>
		<pubDate>Wed, 12 Aug 2009 15:40:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-30526</guid>
		<description>Ben --

So, I played around with your code a bit.  Running your initial code on my laptop, it takes ~1.06 seconds.  If I alter your vectorized code by changing all cases where you indexing into the whole matrix into just using the matrix itself: 

&lt;pre&gt;
% old code 
% Ex2(:,:,:)=(Ex1(:,:,:))+deltaE*(dHzdy(:,:,:)-dHydz(:,:,:));
%
% new code
Ex2 = Ex1+deltaE*(dHzdy -dHydz);
&lt;/pre&gt;

Your vectorized code runs in ~0.78 seconds. 

This is my first shot at vectorization:

&lt;pre&gt;


gridxs = 100;
gridys = 100;
gridzs = 100;
deltaH = 0.1;
deltaE = 0.1;


%%  Vectorized Version
RandStream.setDefaultStream(RandStream(&#039;mt19937ar&#039;,&#039;seed&#039;,1));

Hx = rand(gridxs,gridys,gridzs);
Hy = rand(gridxs,gridys,gridzs);
Hz = rand(gridxs,gridys,gridzs);

Ex = rand(gridxs,gridys,gridzs);
Ey = rand(gridxs,gridys,gridzs);
Ez = rand(gridxs,gridys,gridzs);

Hx22 = zeros(gridxs,gridys,gridzs);
Hy22 = zeros(gridxs,gridys,gridzs);
Hz22 = zeros(gridxs,gridys,gridzs);

Ex22 = zeros(gridxs,gridys,gridzs);
Ey22 = zeros(gridxs,gridys,gridzs);
Ez22 = zeros(gridxs,gridys,gridzs);

tic

% dExdx2 = diff(Ex(2:end-1,2:end-2,2:end-2),1,1);
dExdy2 = diff(Ex(2:end-2,2:end-1,2:end-2),1,2);
dExdz2 = diff(Ex(2:end-2,2:end-2,2:end-1),1,3);

dEydx2 = diff(Ey(2:end-1,2:end-2,2:end-2),1,1);
% dEydy2 = diff(Ey(2:end-2,2:end-1,2:end-2),1,2);
dEydz2 = diff(Ey(2:end-2,2:end-2,2:end-1),1,3);

dEzdx2 = diff(Ez(2:end-1,2:end-2,2:end-2),1,1);
dEzdy2 = diff(Ez(2:end-2,2:end-1,2:end-2),1,2);
% dEzdz2 = diff(Ez(2:end-2,2:end-2,2:end-1),1,3);

Hx22(2:end-2,2:end-2,2:end-2)= Hx(2:end-2,2:end-2,2:end-2)-deltaH*(dEzdy2-dEydz2);
Hy22(2:end-2,2:end-2,2:end-2)= Hy(2:end-2,2:end-2,2:end-2)-deltaH*(dExdz2-dEzdx2);
Hz22(2:end-2,2:end-2,2:end-2)= Hz(2:end-2,2:end-2,2:end-2)-deltaH*(dEydx2-dExdy2);

% dHxdx2 = diff(Hx22(1:end-2,2:end-2,2:end-2),1,1);
dHxdy2 = diff(Hx22(2:end-2,1:end-2,2:end-2),1,2);
dHxdz2 = diff(Hx22(2:end-2,2:end-2,1:end-2),1,3);

dHydx2 = diff(Hy22(1:end-2,2:end-2,2:end-2),1,1);
% dHydy2 = diff(Hy22(2:end-2,1:end-2,2:end-2),1,2);
dHydz2 = diff(Hy22(2:end-2,2:end-2,1:end-2),1,3);

dHzdx2 = diff(Hz22(1:end-2,2:end-2,2:end-2),1,1);
dHzdy2 = diff(Hz22(2:end-2,1:end-2,2:end-2),1,2);
% dHzdz2 = diff(Hz22(2:end-2,2:end-2,1:end-2),1,3);

Ex22(2:end-2,2:end-2,2:end-2)= Ex(2:end-2,2:end-2,2:end-2)+deltaE*(dHzdy2-dHydz2);
Ey22(2:end-2,2:end-2,2:end-2)= Ey(2:end-2,2:end-2,2:end-2)+deltaE*(dHxdz2-dHzdx2);
Ez22(2:end-2,2:end-2,2:end-2)= Ez(2:end-2,2:end-2,2:end-2)+deltaE*(dHydx2-dHxdy2);

&lt;/pre&gt;

My code takes ~0.44 seconds to run.  With more tweaking I could perhaps get it slower, but vectorization does improve the performance considerably. I also compare the answer from your code to mine and they give the same outputs. I used diff because it made for cleaner code. 

Cheers,
Sarah</description>
		<content:encoded><![CDATA[<p>Ben &#8211;</p>
<p>So, I played around with your code a bit.  Running your initial code on my laptop, it takes ~1.06 seconds.  If I alter your vectorized code by changing all cases where you indexing into the whole matrix into just using the matrix itself: </p>
<pre>
% old code
% Ex2(:,:,:)=(Ex1(:,:,:))+deltaE*(dHzdy(:,:,:)-dHydz(:,:,:));
%
% new code
Ex2 = Ex1+deltaE*(dHzdy -dHydz);
</pre>
<p>Your vectorized code runs in ~0.78 seconds. </p>
<p>This is my first shot at vectorization:</p>
<pre>

gridxs = 100;
gridys = 100;
gridzs = 100;
deltaH = 0.1;
deltaE = 0.1;

%%  Vectorized Version
RandStream.setDefaultStream(RandStream('mt19937ar','seed',1));

Hx = rand(gridxs,gridys,gridzs);
Hy = rand(gridxs,gridys,gridzs);
Hz = rand(gridxs,gridys,gridzs);

Ex = rand(gridxs,gridys,gridzs);
Ey = rand(gridxs,gridys,gridzs);
Ez = rand(gridxs,gridys,gridzs);

Hx22 = zeros(gridxs,gridys,gridzs);
Hy22 = zeros(gridxs,gridys,gridzs);
Hz22 = zeros(gridxs,gridys,gridzs);

Ex22 = zeros(gridxs,gridys,gridzs);
Ey22 = zeros(gridxs,gridys,gridzs);
Ez22 = zeros(gridxs,gridys,gridzs);

tic

% dExdx2 = diff(Ex(2:end-1,2:end-2,2:end-2),1,1);
dExdy2 = diff(Ex(2:end-2,2:end-1,2:end-2),1,2);
dExdz2 = diff(Ex(2:end-2,2:end-2,2:end-1),1,3);

dEydx2 = diff(Ey(2:end-1,2:end-2,2:end-2),1,1);
% dEydy2 = diff(Ey(2:end-2,2:end-1,2:end-2),1,2);
dEydz2 = diff(Ey(2:end-2,2:end-2,2:end-1),1,3);

dEzdx2 = diff(Ez(2:end-1,2:end-2,2:end-2),1,1);
dEzdy2 = diff(Ez(2:end-2,2:end-1,2:end-2),1,2);
% dEzdz2 = diff(Ez(2:end-2,2:end-2,2:end-1),1,3);

Hx22(2:end-2,2:end-2,2:end-2)= Hx(2:end-2,2:end-2,2:end-2)-deltaH*(dEzdy2-dEydz2);
Hy22(2:end-2,2:end-2,2:end-2)= Hy(2:end-2,2:end-2,2:end-2)-deltaH*(dExdz2-dEzdx2);
Hz22(2:end-2,2:end-2,2:end-2)= Hz(2:end-2,2:end-2,2:end-2)-deltaH*(dEydx2-dExdy2);

% dHxdx2 = diff(Hx22(1:end-2,2:end-2,2:end-2),1,1);
dHxdy2 = diff(Hx22(2:end-2,1:end-2,2:end-2),1,2);
dHxdz2 = diff(Hx22(2:end-2,2:end-2,1:end-2),1,3);

dHydx2 = diff(Hy22(1:end-2,2:end-2,2:end-2),1,1);
% dHydy2 = diff(Hy22(2:end-2,1:end-2,2:end-2),1,2);
dHydz2 = diff(Hy22(2:end-2,2:end-2,1:end-2),1,3);

dHzdx2 = diff(Hz22(1:end-2,2:end-2,2:end-2),1,1);
dHzdy2 = diff(Hz22(2:end-2,1:end-2,2:end-2),1,2);
% dHzdz2 = diff(Hz22(2:end-2,2:end-2,1:end-2),1,3);

Ex22(2:end-2,2:end-2,2:end-2)= Ex(2:end-2,2:end-2,2:end-2)+deltaE*(dHzdy2-dHydz2);
Ey22(2:end-2,2:end-2,2:end-2)= Ey(2:end-2,2:end-2,2:end-2)+deltaE*(dHxdz2-dHzdx2);
Ez22(2:end-2,2:end-2,2:end-2)= Ez(2:end-2,2:end-2,2:end-2)+deltaE*(dHydx2-dHxdy2);
</pre>
<p>My code takes ~0.44 seconds to run.  With more tweaking I could perhaps get it slower, but vectorization does improve the performance considerably. I also compare the answer from your code to mine and they give the same outputs. I used diff because it made for cleaner code. </p>
<p>Cheers,<br />
Sarah</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: B Cook</title>
		<link>http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-30519</link>
		<dc:creator>B Cook</dc:creator>
		<pubDate>Fri, 07 Aug 2009 11:09:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/#comment-30519</guid>
		<description>Ooops in my previous post the below code should be within the inner loop like all the rest of it 

     &lt;pre&gt;           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                Ex2(i,j,k)= Ex(i,j,k)+deltaE*(dHzdy-dHydz);
                Ey2(i,j,k)= Ey(i,j,k)+deltaE*(dHxdz-dHzdx);
                Ez2(i,j,k)= Ez(i,j,k)+deltaE*(dHydx-dHxdy);

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Ooops in my previous post the below code should be within the inner loop like all the rest of it </p>
<pre>           %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                Ex2(i,j,k)= Ex(i,j,k)+deltaE*(dHzdy-dHydz);
                Ey2(i,j,k)= Ey(i,j,k)+deltaE*(dHxdz-dHzdx);
                Ez2(i,j,k)= Ez(i,j,k)+deltaE*(dHydx-dHxdy);
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

