help-octave
[Top][All Lists]
Advanced

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

RE: trying to optimize my octave program


From: Tim Rueth
Subject: RE: trying to optimize my octave program
Date: Sun, 21 Mar 2010 01:11:26 -0700

>From Judd:
> The trick is to use logical arrays for indexing in the assignment.
> Assuming c_mat is a cell array:
> 
> a_mat = rand([4 5 6 7]) ;
> b_mat = rand([4 5 6 7]) ;
> c_mat = repmat({""},[4 5 6 7]) ;
> 
> c_mat(a_mat < b_mat) = "foo" ;
> 
> Another option is:
> 
> idx = find(a_mat < b_mat) ;
> c_mat(idx) = "foo" ;
> cntr = length(idx) ;
> 

>From Jaroslav:
> mask = a_mat < b_mat;
> c_mat(mask) = {"foo"};
> cntr = sum (mask(:));

Well, it took a while, but I've now completely vectorized my analyze()
function, taking out its for-loop as well as four of the for-loops in the
main script by using ndgrid.  The code is a bit harder to follow now (I'm
used to if-then-else's instead of masking a whole vector at once), but whoa,
what a huge time savings when those vars are "iterated."  For example, I had
ndgrid set up an 81-element matrix for each of the vars that used to have a
for-loop.  With the for-loop, it took 429 sec to do these 81 iterations (5.3
sec/iteration).  Now with the vectorization using ndgrid, all 81 passes are
done in parallel, taking just 15.5 sec (equivalent to 0.2 sec/iteration).
Thanks again for the great help guys!

I guess I could go further and peel off some more of those 3 remaining
for-loops, but these have quite a few iterations associated with them, and
so the corresponding ndgrid could get astronomically huge.

--Tim



reply via email to

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