help-octave
[Top][All Lists]

## Re: vectorization quiz

 From: Jaroslav Hajek Subject: Re: vectorization quiz Date: Fri, 10 Sep 2010 10:49:16 +0200

```On Fri, Sep 10, 2010 at 10:42 AM, Francesco Potortì <address@hidden> wrote:
>>> I thought a little, but I do not see the way of vectorising this.  It's
>>> a loop that is run only few times, but is called very frequently.
>>> Essentially, there are few 2-D maps stacked one over the other which
>>> must be shifted each by a variable amount and then summed to obtain a
>>> new 2-D map.  Here is the code:
>>>
>>>    for mi = 1:m
>>>      llka += llkm(by+offm(mi,1), bx+offm(mi,2), mi);
>>>    endfor
>>>
>>> where:
>>> m          ==>     5
>>> size(llka) ==>     156 222
>>> size(llkm) ==>     186 252 5
>>> by         ==>     16:172
>>> bx         ==>     16:238
>>> size(offm) ==>     5 2
>>> offm(m,i)  ==>     a scalar in the range [-15;15]
>>>
>>> Thanks in advance to those wishing to try :)
>>>
>>I don't think there's a good way. You can, of course, extract the
>>submatrices via cellfun and then use plus, but it's not going to be
>>significantly better.
>
> I think the slow thing is the index computation, so maybe I could try to
> compute them all at once and then index, reshape and sum.  Maybe I'll try.
>

Is it? range + scalar should result in a range when possible. I think
this already worked in Octave 3.2.4.
Indexing with ranges is generally the fastest option.

> With the new diag implementation now we have a fast way of multiplying
> each row of a matrix by a different number.  But I think we have no
> euivalently fast way of summing a different number to each row.  Am I
> wrong?
>

In 3.3.52+, you can use bsxfun (@plus, a, b).

>>                      Given that m = 5, I think it's not even worth
>>trying. If it doesn't seem to be possible to vectorize (part of) the
>>outer loop, your best approach is to write a compiled function.
>>Perhaps a sufficiently general one would be useful in Octave or
>>OctaveForge.
>

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