help-octave
[Top][All Lists]
Advanced

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

Re: Vectorizing code array modulo indexing


From: Jordi Gutiérrez Hermoso
Subject: Re: Vectorizing code array modulo indexing
Date: Wed, 3 Oct 2012 11:47:11 -0400

On 2 October 2012 15:00, Torbjörn Rathsman <address@hidden> 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.

I really don't see the problem. The following code runs for me without
error:

    N = 100;
    f = [1:N] + rand(1,N);
    spectrum = [1:N] + rand(1,N);
    s = spectrum;

    notes=[1 1 0 1 1 1 0 1 1 0 1 0];

    x = 12*log(f/440)/log(2);
    spectrum .*= cos(pi*x).^16.*notes( mod(round(x),12) + 1 );

    for k=1:N %N huge
      x = 12*log(f(k)/440)/log(2);
      s(k) *= cos(pi*x).^16.* notes( mod(round(x),12) + 1 );
    endfor

    norm(s-spectrum)

If my f and spectrum variables are making assumptions different from
your own, please give me variables for f and spectrum I can use to
test.

HTH,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]