[GRASS-dev] anyone interested in helping to port an algorithm from matlab?

Hamish hamish_b at yahoo.com
Wed Nov 5 22:39:32 EST 2008


Dylan Beaudette wrote:
> # accumulate new quadrants
> # WTF ?
> cc = [cc;cord]
> Qc=[Qc;Qh]
> 
> if cc is matrix, and cord is a matrix, what exactly is
> happening?

"coord" is appended to the end of "cc". e.g.:

>> a = [ 10 20 ]
>> b = [ 15 25 ]
>> c = [a; b]
c =

    10    20
    15    25

>> d = [ a; b; a ]
d =

    10    20
    15    25
    10    20


> I have a sneaking suspicion that this algorithm is not very efficient
> (the fortran version), as it takes quite a while (well 3 minutes) to
> process a 400x400 grid.

in the above example, appending the variable has the effect of resizing
the array, which is very very slow. Whenever possible it is better to
determine/guess the final size of the array and create it (full of NaNs)
and populate it row by row. "cc = [cc; next]" will get slower and slower
as the iterations grow. You can crop off any leftover NaNs once finished.

many "for" loops in matlab can be converted to array calcs greatly
speeding things up. if things are done right, nested for loops should
be a rare sight.

matlab disk/sscanf i/o has traditionally been much much slower than C,
although I think this has somewhat improved with newer versions (or it
just seems that way because the CPUs get faster and faster?). I usually
try to use UNIX tools (sed/awk/...) to pre-process ascii data before
load()ing into matlab. you can use !cmd or system() calls in matlab to
do that within the script.


The Fortran compiler you use can greatly affect run time. Using something
modern?



Hamish



      



More information about the grass-dev mailing list