help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Octave too slow runnig some instruction


From: Jaroslav Hajek
Subject: Re: Octave too slow runnig some instruction
Date: Thu, 24 Jun 2010 13:18:33 +0200

On Thu, Jun 24, 2010 at 12:00 PM, sd83 <address@hidden> wrote:
>
> Oh, I'm sorry. This is my m-file'code:
>
> function h=make_random_direction(n,N,sigma)
>
> x=zeros(N,1);
> y=zeros(N,1);
>
> for i=1:N
> x(i)=normrnd (0,sigma(i)*sigma(i),1,1);
> y(i)=normrnd (0,sigma(i)*sigma(i),1,1);
> end
>
> h=zeros(n,2);
> t=linspace(0,2*pi,n);
>
> for i=1:n
>        for j=1:N
> h(i,1)=h(i,1)+x(j)*cos(t(i)*j);
> h(i,2)=h(i,2)+y(j)*sin(t(i)*j);
>        end
> end
>
>
> end
>
>

If you're a novice with Octave/Matlab, try retraining yourself to a
vectorized coding style.
Use matrix and n-dimensional operations where possible.

x = normrnd (0, sigma.^2, N, 1);
y = normrnd (0, sigma.^2, N, 1);

t = linspace (0, 2*pi, n);
tt = t.' * (1:N); # outer product
h = [cos(tt)*x, sin(tt)*y];


besides being faster, you'll also find your code being simpler in this way.

hth


-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]