help-octave
[Top][All Lists]
Advanced

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

Re: Vectorization of a random function


From: Nicholas Jankowski
Subject: Re: Vectorization of a random function
Date: Wed, 27 Jan 2016 11:03:59 -0500

On Wed, Jan 27, 2016 at 7:33 AM, altamiranomaxi <address@hidden> wrote:
Hi!

I'm trying to vectorize the following code:

g=zeros(ensayos,1);
for i=1:ensayos
        g(i) = TiempoMuerte_Cte(N,c);
endfor

Where TiempoMuerte_Cte(N,c) is a function that does not give always the same
result, so I must run this function "ensayos" times: I've tried to write

g=zeros(ensayos,1);
g(:,1)= TiempoMuerte_Cte(N,c);

but it just computes the function once, and then repeats the value in every
coordinate of "g"



My guess is that TiempoMuerte_Cte returns a scalar.  in that case, your vectorized attempt is the same as saying:

Q = TiempoMuerte_Cte(N,c);
g(:,1) = Q;

you will just be assigning that scalar to every position in the first column of g.

if you want to vectorize this, TiempoMuerte_Cte must be vectorized.  You will need to pass it something that lets it know the size of the array to return, and then as long as it's the same size as g, it'll fill in the way you wanted.  Note that if you just rewrite TiempoMuerte to loop through the array, you won't see that much speed benefit over looping outside the function.   (maybe a bit of overhead improvemen, but nothing like you'd get by actually vectorizing the process). I've heard that arrayfun doesn't actually provide much speedup for most people, it just reduces the need to write fewer explicit for loops in the code.

nickj

reply via email to

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