Re: Vectorizing code array modulo indexing

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Vectorizing code array modulo indexing

Juan Pablo Carbajal
On Wed, Oct 3, 2012 at 12:17 PM, Torbjörn Rathsman
<[hidden email]> wrote:

> 2012-10-02 22:12, Juan Pablo Carbajal skrev:
>> On Tue, Oct 2, 2012 at 9:00 PM, Torbjörn Rathsman
>> <[hidden email]> wrote:
>>> Is this kind of thing possible to do without do it in a loop?
>>>
>>>     notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>>     x=12.*log(f./440)./log(2);
>>>     spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>>>
>>> f is a vector of 10^7 elements and I want to take each computed index and
>>> generate corresponding value from notes.
>>> _______________________________________________
>>> Help-octave mailing list
>>> [hidden email]
>>> https://mailman.cae.wisc.edu/listinfo/help-octave
>> Can you give more details? I do not see a loop.
>> I see that you are multiplying spectrum by something and then saving
>> it back into spectrum. What is the initial value of spectrum.
>>
>> Sorry for not understanding.
>>
>>
> Implemented using a loop:
>
> notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>
> for k=1:N %N huge
>
> x=12.*log(f(k)./440)./log(2);
>     spectrum(k)=spectrum(k).*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>
> end
>
> I want to implement the same thing without a loop.
>

Hi,

Always keep the mailing list in CC.

Here is my attempt tp vectorize your code. I expanded it so that you
understand all the stages. You do not need to create all those
intermediate variables
http://agora.octave.org/snippet/hchr/

Note that I am assuming that the variable spectrum exists, it is a
column and has length N.

Hope this helps.


--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Octave-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/octave-dev
Reply | Threaded
Open this post in threaded view
|

Re: Vectorizing code array modulo indexing

Torbjörn Rathsman
Juan Pablo Carbajal skrev 2012-10-03 14:02:

> On Wed, Oct 3, 2012 at 12:17 PM, Torbjörn Rathsman
> <[hidden email]> wrote:
>> 2012-10-02 22:12, Juan Pablo Carbajal skrev:
>>> On Tue, Oct 2, 2012 at 9:00 PM, Torbjörn Rathsman
>>> <[hidden email]> wrote:
>>>> Is this kind of thing possible to do without do it in a loop?
>>>>
>>>>      notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>>>      x=12.*log(f./440)./log(2);
>>>>      spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>>>>
>>>> f is a vector of 10^7 elements and I want to take each computed index and
>>>> generate corresponding value from notes.
>>>> _______________________________________________
>>>> Help-octave mailing list
>>>> [hidden email]
>>>> https://mailman.cae.wisc.edu/listinfo/help-octave
>>> Can you give more details? I do not see a loop.
>>> I see that you are multiplying spectrum by something and then saving
>>> it back into spectrum. What is the initial value of spectrum.
>>>
>>> Sorry for not understanding.
>>>
>>>
>> Implemented using a loop:
>>
>> notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>
>> for k=1:N %N huge
>>
>> x=12.*log(f(k)./440)./log(2);
>>      spectrum(k)=spectrum(k).*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>>
>> end
>>
>> I want to implement the same thing without a loop.
>>
> Hi,
>
> Always keep the mailing list in CC.
>
> Here is my attempt tp vectorize your code. I expanded it so that you
> understand all the stages. You do not need to create all those
> intermediate variables
> http://agora.octave.org/snippet/hchr/
>
> Note that I am assuming that the variable spectrum exists, it is a
> column and has length N.
>
> Hope this helps.
>
>
It does not work. You forgot that f is a vector, so is x and x_mod. Now,
trying to use x_mod as index, will not expand notes into a squarewavish
function but just fail with

  subscript indices must be either positive integers or logicals

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Octave-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/octave-dev
Reply | Threaded
Open this post in threaded view
|

Re: Vectorizing code array modulo indexing

Juan Pablo Carbajal
On Wed, Oct 3, 2012 at 4:55 PM, Torbjörn Rathsman
<[hidden email]> wrote:

> Juan Pablo Carbajal skrev 2012-10-03 14:02:
>
>> On Wed, Oct 3, 2012 at 12:17 PM, Torbjörn Rathsman
>> <[hidden email]> wrote:
>>>
>>> 2012-10-02 22:12, Juan Pablo Carbajal skrev:
>>>>
>>>> On Tue, Oct 2, 2012 at 9:00 PM, Torbjörn Rathsman
>>>> <[hidden email]> wrote:
>>>>>
>>>>> Is this kind of thing possible to do without do it in a loop?
>>>>>
>>>>>      notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>>>>      x=12.*log(f./440)./log(2);
>>>>>      spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>>>>>
>>>>> f is a vector of 10^7 elements and I want to take each computed index
>>>>> and
>>>>> generate corresponding value from notes.
>>>>> _______________________________________________
>>>>> Help-octave mailing list
>>>>> [hidden email]
>>>>> https://mailman.cae.wisc.edu/listinfo/help-octave
>>>>
>>>> Can you give more details? I do not see a loop.
>>>> I see that you are multiplying spectrum by something and then saving
>>>> it back into spectrum. What is the initial value of spectrum.
>>>>
>>>> Sorry for not understanding.
>>>>
>>>>
>>> Implemented using a loop:
>>>
>>> notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>>
>>> for k=1:N %N huge
>>>
>>> x=12.*log(f(k)./440)./log(2);
>>>      spectrum(k)=spectrum(k).*cos(pi.*x).^16.* notes( mod(round(x),12) +
>>> 1 );
>>>
>>> end
>>>
>>> I want to implement the same thing without a loop.
>>>
>> Hi,
>>
>> Always keep the mailing list in CC.
>>
>> Here is my attempt tp vectorize your code. I expanded it so that you
>> understand all the stages. You do not need to create all those
>> intermediate variables
>> http://agora.octave.org/snippet/hchr/
>>
>> Note that I am assuming that the variable spectrum exists, it is a
>> column and has length N.
>>
>> Hope this helps.
>>
>>
> It does not work. You forgot that f is a vector, so is x and x_mod. Now,
> trying to use x_mod as index, will not expand notes into a squarewavish
> function but just fail with
>
>  subscript indices must be either positive integers or logicals

Hi,

It works here. Can you please post your complete example in Agora
(agora.octave.org) so we can see where is the problem?

Thanks


--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Octave-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/octave-dev
Reply | Threaded
Open this post in threaded view
|

Re: Vectorizing code array modulo indexing

Juan Pablo Carbajal
On Wed, Oct 3, 2012 at 6:01 PM, Torbjörn Rathsman
<[hidden email]> wrote:

> Juan Pablo Carbajal skrev 2012-10-03 17:48:
>
>> On Wed, Oct 3, 2012 at 4:55 PM, Torbjörn Rathsman
>> <[hidden email]> wrote:
>>>
>>> Juan Pablo Carbajal skrev 2012-10-03 14:02:
>>>
>>>> On Wed, Oct 3, 2012 at 12:17 PM, Torbjörn Rathsman
>>>> <[hidden email]> wrote:
>>>>>
>>>>> 2012-10-02 22:12, Juan Pablo Carbajal skrev:
>>>>>>
>>>>>> On Tue, Oct 2, 2012 at 9:00 PM, Torbjörn Rathsman
>>>>>> <[hidden email]> wrote:
>>>>>>>
>>>>>>> Is this kind of thing possible to do without do it in a loop?
>>>>>>>
>>>>>>>       notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>>>>>>       x=12.*log(f./440)./log(2);
>>>>>>>       spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1
>>>>>>> );
>>>>>>>
>>>>>>> f is a vector of 10^7 elements and I want to take each computed index
>>>>>>> and
>>>>>>> generate corresponding value from notes.
>>>>>>> _______________________________________________
>>>>>>> Help-octave mailing list
>>>>>>> [hidden email]
>>>>>>> https://mailman.cae.wisc.edu/listinfo/help-octave
>>>>>>
>>>>>> Can you give more details? I do not see a loop.
>>>>>> I see that you are multiplying spectrum by something and then saving
>>>>>> it back into spectrum. What is the initial value of spectrum.
>>>>>>
>>>>>> Sorry for not understanding.
>>>>>>
>>>>>>
>>>>> Implemented using a loop:
>>>>>
>>>>> notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>>>>>
>>>>> for k=1:N %N huge
>>>>>
>>>>> x=12.*log(f(k)./440)./log(2);
>>>>>       spectrum(k)=spectrum(k).*cos(pi.*x).^16.* notes( mod(round(x),12)
>>>>> +
>>>>> 1 );
>>>>>
>>>>> end
>>>>>
>>>>> I want to implement the same thing without a loop.
>>>>>
>>>> Hi,
>>>>
>>>> Always keep the mailing list in CC.
>>>>
>>>> Here is my attempt tp vectorize your code. I expanded it so that you
>>>> understand all the stages. You do not need to create all those
>>>> intermediate variables
>>>> http://agora.octave.org/snippet/hchr/
>>>>
>>>> Note that I am assuming that the variable spectrum exists, it is a
>>>> column and has length N.
>>>>
>>>> Hope this helps.
>>>>
>>>>
>>> It does not work. You forgot that f is a vector, so is x and x_mod. Now,
>>> trying to use x_mod as index, will not expand notes into a squarewavish
>>> function but just fail with
>>>
>>>   subscript indices must be either positive integers or logicals
>>
>> Hi,
>>
>> It works here. Can you please post your complete example in Agora
>> (agora.octave.org) so we can see where is the problem?
>>
>> Thanks
>>
>>
> It works, I think I accidently put -inf in x log(0) !

Solution working
http://agora.octave.org/snippet/hchr/

--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/

------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
Octave-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/octave-dev