[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: vectorization quiz
From: |
Jaroslav Hajek |
Subject: |
Re: vectorization quiz |
Date: |
Fri, 10 Sep 2010 11:54:44 +0200 |
On Fri, Sep 10, 2010 at 11:12 AM, Francesco Potortì <address@hidden> wrote:
>>
>>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.
>
> I am referring to the fact that the same thing is done several times
> because of the loop. But in fact, if you precompute indexes then you
> lose the advantage of having ranges as indexes. Mh.
>
>>> 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).
>
> It also works in 3.2.4:
>
> octave> bsxfun(@plus,1:7,(1:3)')
> ans =
>
> 2 3 4 5 6 7 8
> 3 4 5 6 7 8 9
> 4 5 6 7 8 9 10
>
> At last I understood what's the use of bsxfun! Thanks :)
>
Yes, but you'll find out that in 3.2.4 the bsxfun expression is much
slower than using repmat and ordinary addition.
In 3.3.52+, bsxfun is optimized for certain built-in function handles.
regards
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz