# Re: Vectorizing code array modulo indexing

4 messages
Open this post in threaded view
|

## Re: Vectorizing code array modulo indexing

 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
Open this post in threaded view
|

## Re: Vectorizing code array modulo indexing

 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