help-octave
[Top][All Lists]
Advanced

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

Re: help requested with extremely slow for loop?


From: Jaroslav Hajek
Subject: Re: help requested with extremely slow for loop?
Date: Thu, 25 Mar 2010 11:27:01 +0100

2010/3/25 Joachim De Beule <address@hidden>:
>
> Many thanks to all of you, this is really interesting!
>
> Are there any other 'insiders' I should know about with respect to sparse
> matrices? (I've read the 'Mathematical considerations' in the info file.) In
> particular, could you please expand on the following because I am not sure
> what it means.
>
>> This should work well unless there are loop-carried dependencies
>> assumed, in which case you'll need something more elaborate.
>

The code first extracts all x(idxs), then adds this to x(y(idxs)),
then sets x(idxs) to zero.

x(y(idxs)) += x(idxs);
x(idxs)=0;

This is equivalent to your former code

       for j = idxs
               x(y(j)) += x(j);
               x(j) = 0;
       endfor

only if, say, y(j) >= j for all j. Otherwise the value of x(j) in a
given cycle may be affected by an earlier cycle, which the vectorized
version won't do. To sucessfuly use Octave you need to retrain your
mind a little to vectorized thinking.

>
>> If you have thousands of independent vectors, you may also consider
>> stacking them in a matrix.
>
> So you mean sth like xs = [x1 x2 ... x3], with the xi one of the x's in the
> previous code? Actually that is what I have, but I am not sure what that gives
> me with respect to the calculations, I still need a for loop:
>
> for i = 1:length(xs)
>        idxs = find(xs(:,i) & y);
>        xs(:,idxs(i)) += xs(:,i);
>        xs(:,i) = 0;
> endfor
>
> Or am I missing something again?
>
> Joachim
>

Well, maybe it can be done more efficiently if xs has lots of columns.
It doesn't seem the above code is correct, though, or at least it
doesn't quite correspond to what you posted before.

-- 
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]