http://www.mathworks.com/matlabcentral/newsreader/view_thread/307148
MATLAB Central Newsreader  Avoiding for loops
Feed for thread: Avoiding for loops
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 02 May 2011 16:30:26 +0000
Avoiding for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307148#834027
Lan
Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?<br>
<br>
z0 is a n x 1 vector (n=2000)<br>
<br>
for n=1:length(z0)<br>
X=100:2/100:100;<br>
Y=X.*normpdf(X,(0.6*z0(n)),0.64);<br>
M0(n) = trapz(X,Y);<br>
end<br>
<br>
Thanks

Tue, 03 May 2011 07:12:04 +0000
Re: Avoiding for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307148#834141
Roger Stafford
"Lan" wrote in message <ipmm71$p7b$1@fred.mathworks.com>...<br>
> Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?<br>
> <br>
> z0 is a n x 1 vector (n=2000)<br>
> <br>
> for n=1:length(z0)<br>
> X=100:2/100:100;<br>
> Y=X.*normpdf(X,(0.6*z0(n)),0.64);<br>
> M0(n) = trapz(X,Y);<br>
> end<br>
> <br>
> Thanks<br>
        <br>
If your aim is to vectorize this precise computation, I'm afraid it would involve some 2000 by 10001 size matrices which could give you memory troubles. You could make use of 'ndgrid' for this purpose, but I won't write out the explicit expression because I seriously doubt that this is what you really want to do.<br>
<br>
I am guessing you are using 'trapz' to approximate the integral of the above expression for Y between 100 and +100 for each of the mean values in mu = 0.6*z0. If that is the case, you can easily obtain a vectorized solution involving all z0 in terms of the 'normpdf' and 'normcdf' functions. It would look like this:<br>
<br>
M0 = sigma^2*(normpdf(b,mu,sigma)normpdf(a,mu,sigma)) ...<br>
+ mu.*(normcdf(b,mu,sigma)normcdf(a,mu,sigma));<br>
<br>
where mu = 0.6*z0, sigma = 0.64, b = 100, and a = 100. There would then be no need for involving all those ten thousand and one values in X.<br>
<br>
You can derive this by writing the expression for integrand Y as:<br>
<br>
x*normpdf(x,mu,sigma) = (xmu)*normpdf(x,mu,sigma) + mu*normpdf(x,mu,sigma)<br>
<br>
The first of these terms can easily be integrated to sigma^2 times just 'normpdf', while the integral of the second can be evaluated in terms of the cumulative normal distribution values, 'normcdf'. (You had better check my algebra and calculus here for possible mistakes, but I think the basic method is correct.)<br>
<br>
Roger Stafford

Wed, 04 May 2011 11:24:04 +0000
Re: Avoiding for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307148#834378
Lan
"Roger Stafford" wrote in message <ipo9s4$rkm$1@fred.mathworks.com>...<br>
> "Lan" wrote in message <ipmm71$p7b$1@fred.mathworks.com>...<br>
> > Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?<br>
> > <br>
> > z0 is a n x 1 vector (n=2000)<br>
> > <br>
> > for n=1:length(z0)<br>
> > X=100:2/100:100;<br>
> > Y=X.*normpdf(X,(0.6*z0(n)),0.64);<br>
> > M0(n) = trapz(X,Y);<br>
> > end<br>
> > <br>
> > Thanks<br>
>         <br>
> If your aim is to vectorize this precise computation, I'm afraid it would involve some 2000 by 10001 size matrices which could give you memory troubles. You could make use of 'ndgrid' for this purpose, but I won't write out the explicit expression because I seriously doubt that this is what you really want to do.<br>
> <br>
> I am guessing you are using 'trapz' to approximate the integral of the above expression for Y between 100 and +100 for each of the mean values in mu = 0.6*z0. If that is the case, you can easily obtain a vectorized solution involving all z0 in terms of the 'normpdf' and 'normcdf' functions. It would look like this:<br>
> <br>
> M0 = sigma^2*(normpdf(b,mu,sigma)normpdf(a,mu,sigma)) ...<br>
> + mu.*(normcdf(b,mu,sigma)normcdf(a,mu,sigma));<br>
> <br>
> where mu = 0.6*z0, sigma = 0.64, b = 100, and a = 100. There would then be no need for involving all those ten thousand and one values in X.<br>
> <br>
> You can derive this by writing the expression for integrand Y as:<br>
> <br>
> x*normpdf(x,mu,sigma) = (xmu)*normpdf(x,mu,sigma) + mu*normpdf(x,mu,sigma)<br>
> <br>
> The first of these terms can easily be integrated to sigma^2 times just 'normpdf', while the integral of the second can be evaluated in terms of the cumulative normal distribution values, 'normcdf'. (You had better check my algebra and calculus here for possible mistakes, but I think the basic method is correct.)<br>
> <br>
> Roger Stafford<br>
<br>
Thanks!<br>
<br>
But do I understand it correctly that I still have to loop over the length of z0? since my mu changes for each element of z0. I want to obtain a vector M0 of dimensions 2000x 1.<br>
<br>
Lan<br>

Wed, 04 May 2011 11:25:20 +0000
Re: Avoiding for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307148#834379
Lan
"Roger Stafford" wrote in message <ipo9s4$rkm$1@fred.mathworks.com>...<br>
> "Lan" wrote in message <ipmm71$p7b$1@fred.mathworks.com>...<br>
> > Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?<br>
> > <br>
> > z0 is a n x 1 vector (n=2000)<br>
> > <br>
> > for n=1:length(z0)<br>
> > X=100:2/100:100;<br>
> > Y=X.*normpdf(X,(0.6*z0(n)),0.64);<br>
> > M0(n) = trapz(X,Y);<br>
> > end<br>
> > <br>
> > Thanks<br>
>         <br>
> If your aim is to vectorize this precise computation, I'm afraid it would involve some 2000 by 10001 size matrices which could give you memory troubles. You could make use of 'ndgrid' for this purpose, but I won't write out the explicit expression because I seriously doubt that this is what you really want to do.<br>
> <br>
> I am guessing you are using 'trapz' to approximate the integral of the above expression for Y between 100 and +100 for each of the mean values in mu = 0.6*z0. If that is the case, you can easily obtain a vectorized solution involving all z0 in terms of the 'normpdf' and 'normcdf' functions. It would look like this:<br>
> <br>
> M0 = sigma^2*(normpdf(b,mu,sigma)normpdf(a,mu,sigma)) ...<br>
> + mu.*(normcdf(b,mu,sigma)normcdf(a,mu,sigma));<br>
> <br>
> where mu = 0.6*z0, sigma = 0.64, b = 100, and a = 100. There would then be no need for involving all those ten thousand and one values in X.<br>
> <br>
> You can derive this by writing the expression for integrand Y as:<br>
> <br>
> x*normpdf(x,mu,sigma) = (xmu)*normpdf(x,mu,sigma) + mu*normpdf(x,mu,sigma)<br>
> <br>
> The first of these terms can easily be integrated to sigma^2 times just 'normpdf', while the integral of the second can be evaluated in terms of the cumulative normal distribution values, 'normcdf'. (You had better check my algebra and calculus here for possible mistakes, but I think the basic method is correct.)<br>
> <br>
> Roger Stafford<br>
<br>
Thanks!<br>
<br>
But do I understand it correctly that I still have to loop over the length of z0? since my mu changes for each element of z0. I want to obtain a vector M0 of dimensions 2000x 1.<br>
<br>
Lan<br>

Wed, 04 May 2011 18:05:08 +0000
Re: Avoiding for loops
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307148#834444
Roger Stafford
"Lan" wrote in message <iprd0k$geb$1@fred.mathworks.com>...<br>
> But do I understand it correctly that I still have to loop over the length of z0? since my mu changes for each element of z0. I want to obtain a vector M0 of dimensions 2000x 1.<br>
> Lan<br>
       <br>
No, that is not true. The normpdf and normcdf functions can both accept vector arguments for mu, and therefore you do not have to loop through z0.<br>
<br>
The documentation for these functions states that: "X, mu, and sigma can be vectors, matrices, or multidimensional arrays that all have the same size. A scalar input is expanded to a constant array with the same dimensions as the other inputs."<br>
<br>
Roger Stafford