Thread Subject: improving speed

Subject: improving speed

From: Ci Griaal

Date: 21 Oct, 2009 01:19:02

Message: 1 of 4

Hi
I've written a very simple code that samples a random numbers from a normal distribution using a different mean and standard deviation at each time. It also substitute the negative values by zero.

load('.\data');
randn('state',sum(100*clock));
 for i=1:length(x)
        z(i) = ( randn(1,1) * x(i) ) + y(i);
            if z(i)<0;
            z(i)=0;
            end
 end

In which data.mat contains two .mat with single columns (x and y)
The code works well, however I am dealing with huge files and really need to run it quicker. I know I can improve the performance using
i=1:length(x)
z = ( randn(1,1) * x ) + y

instead of the for loop, but I don’t know how to deal with the if in this case.
Any tips please?

Subject: improving speed

From: dpb

Date: 21 Oct, 2009 01:42:13

Message: 2 of 4

Ci Griaal wrote:
> Hi
> I've written a very simple code that samples a random numbers from a
normal distribution using a different mean and standard deviation at
each time. It also substitute the negative values by zero.
e>
> load('.\data');
> randn('state',sum(100*clock));
> for i=1:length(x)
> z(i) = ( randn(1,1) * x(i) ) + y(i);
> if z(i)<0;
> z(i)=0;
> end
> end

z = randn(size(x)).*x + y;
z(z<0)=0;

help times

--

Subject: improving speed

From: Ci Griaal

Date: 21 Oct, 2009 02:00:05

Message: 3 of 4

dpb <none@non.net> wrote in message <hblp73$bsk$1@news.eternal-september.org>...
> Ci Griaal wrote:
> > Hi
> > I've written a very simple code that samples a random numbers from a
> normal distribution using a different mean and standard deviation at
> each time. It also substitute the negative values by zero.
> e>
> > load('.\data');
> > randn('state',sum(100*clock));
> > for i=1:length(x)
> > z(i) = ( randn(1,1) * x(i) ) + y(i);
> > if z(i)<0;
> > z(i)=0;
> > end
> > end
>
> z = randn(size(x)).*x + y;
> z(z<0)=0;
>
> help times
>
Thanks!

Subject: improving speed

From: arun

Date: 21 Oct, 2009 08:54:21

Message: 4 of 4

On Oct 21, 3:19 am, "Ci Griaal" <cigri...@yahoo.com.au> wrote:
> Hi
> I've written a very simple code that samples a random numbers from a normal distribution using a different mean and standard deviation at each time. It also substitute the negative values by zero.
>
> load('.\data');
> randn('state',sum(100*clock));  
>  for i=1:length(x)
>         z(i) = ( randn(1,1) * x(i) ) + y(i);
>             if z(i)<0;
>             z(i)=0;
>             end
>  end
>
> In which data.mat contains two .mat with single columns (x and y)
> The code works well, however I am dealing with huge files and really need to run it quicker. I know I can improve the performance using  
> i=1:length(x)
> z = ( randn(1,1) * x ) + y
>
> instead of the for loop, but I don’t know how to deal with the if in this case.
> Any tips please?

On a side note, because you are replacing negative values with 0, you
should know that the values you are generating then don't come from
the normal distribution anymore... (at least in a strict sense).
best, arun.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com