Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Avoiding for loops

Subject: Avoiding for loops

From: Lan

Date: 2 May, 2011 16:30:26

Message: 1 of 5

Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?

z0 is a n x 1 vector (n=2000)

for n=1:length(z0)
    X=-100:2/100:100;
    Y=X.*normpdf(X,(0.6*z0(n)),0.64);
    M0(n) = trapz(X,Y);
end

Thanks

Subject: Avoiding for loops

From: Roger Stafford

Date: 3 May, 2011 07:12:04

Message: 2 of 5

"Lan" wrote in message <ipmm71$p7b$1@fred.mathworks.com>...
> Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?
>
> z0 is a n x 1 vector (n=2000)
>
> for n=1:length(z0)
> X=-100:2/100:100;
> Y=X.*normpdf(X,(0.6*z0(n)),0.64);
> M0(n) = trapz(X,Y);
> end
>
> Thanks
- - - - - - - - -
  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.

  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:

 M0 = -sigma^2*(normpdf(b,mu,sigma)-normpdf(a,mu,sigma)) ...
      + mu.*(normcdf(b,mu,sigma)-normcdf(a,mu,sigma));

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.

  You can derive this by writing the expression for integrand Y as:

 x*normpdf(x,mu,sigma) = (x-mu)*normpdf(x,mu,sigma) + mu*normpdf(x,mu,sigma)

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

Roger Stafford

Subject: Avoiding for loops

From: Lan

Date: 4 May, 2011 11:24:04

Message: 3 of 5

"Roger Stafford" wrote in message <ipo9s4$rkm$1@fred.mathworks.com>...
> "Lan" wrote in message <ipmm71$p7b$1@fred.mathworks.com>...
> > Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?
> >
> > z0 is a n x 1 vector (n=2000)
> >
> > for n=1:length(z0)
> > X=-100:2/100:100;
> > Y=X.*normpdf(X,(0.6*z0(n)),0.64);
> > M0(n) = trapz(X,Y);
> > end
> >
> > Thanks
> - - - - - - - - -
> 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.
>
> 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:
>
> M0 = -sigma^2*(normpdf(b,mu,sigma)-normpdf(a,mu,sigma)) ...
> + mu.*(normcdf(b,mu,sigma)-normcdf(a,mu,sigma));
>
> 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.
>
> You can derive this by writing the expression for integrand Y as:
>
> x*normpdf(x,mu,sigma) = (x-mu)*normpdf(x,mu,sigma) + mu*normpdf(x,mu,sigma)
>
> 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.)
>
> Roger Stafford

Thanks!

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.

Lan
 

Subject: Avoiding for loops

From: Lan

Date: 4 May, 2011 11:25:20

Message: 4 of 5

"Roger Stafford" wrote in message <ipo9s4$rkm$1@fred.mathworks.com>...
> "Lan" wrote in message <ipmm71$p7b$1@fred.mathworks.com>...
> > Is there a way to vectorize the for loop below? How can I use normpdf with a vector input?
> >
> > z0 is a n x 1 vector (n=2000)
> >
> > for n=1:length(z0)
> > X=-100:2/100:100;
> > Y=X.*normpdf(X,(0.6*z0(n)),0.64);
> > M0(n) = trapz(X,Y);
> > end
> >
> > Thanks
> - - - - - - - - -
> 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.
>
> 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:
>
> M0 = -sigma^2*(normpdf(b,mu,sigma)-normpdf(a,mu,sigma)) ...
> + mu.*(normcdf(b,mu,sigma)-normcdf(a,mu,sigma));
>
> 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.
>
> You can derive this by writing the expression for integrand Y as:
>
> x*normpdf(x,mu,sigma) = (x-mu)*normpdf(x,mu,sigma) + mu*normpdf(x,mu,sigma)
>
> 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.)
>
> Roger Stafford

Thanks!

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.

Lan
 

Subject: Avoiding for loops

From: Roger Stafford

Date: 4 May, 2011 18:05:08

Message: 5 of 5

"Lan" wrote in message <iprd0k$geb$1@fred.mathworks.com>...
> 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.
> Lan
- - - - - - - -
  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.

  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."

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us