help-octave
[Top][All Lists]
Advanced

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

Re: help with vectorizing a for loop


From: Rick T
Subject: Re: help with vectorizing a for loop
Date: Tue, 4 Oct 2011 16:20:02 -1000

Thanks for all the great help and code I've got it down to 9 seconds still get an error "nonconformant arguments (op1 is 1x15679, op2 is 8193x1)"
using the code below but more than likely I would run into memory errors latter on down the line since the arrays will only get bigger not smaller.

clear all
inner_freq = rand(8193,3);
t = rand(1,15679);
aa_sig_combined = 0;
tic,
aa_sig_combined2 = sum(inner_freq(:, 2)*cos (2*pi*t.*inner_freq(:, 1)+ inner_freq(:, 3)));
toc

But 9 seconds is nothing to shake a stick at

Thanks again all!!!


On Tue, Oct 4, 2011 at 1:06 PM, Martin Helm <address@hidden> wrote:
Am Dienstag, den 04.10.2011, 23:47 +0200 schrieb Martin Helm:
> Am Dienstag, den 04.10.2011, 11:20 -1000 schrieb Rick T:
> > Sorry about that t_rebuilt should be t.
> >
> It's a bit late here so I hope I did not completely mess this up now
>
> aa_sig_combined=sum(diag(inner_freq(1:end-1,2))*cos(2.*pi.*inner_freq(1:end-1,1)*t).+ repmat(inner_freq(1:end-1,3),[1 length(t)]));
>
> takes roughly 77 seconds on my netbook but memory consumption goes up to
> about 3 gig for the octave process (so it uses swap here, not sure what
> happens on windows).
>
>
I thought again about that and excessive use of memory can be avoide by splitting the calculation into some chunks

# just as example use random values
t=rand(1,15679);
inner_freq=rand(8193,3);

N=4; # use 4 chunks
nn = int32(linspace(1, length(t)+1, N+1))
aa_sig_combined=zeros(size(t));
for ii=1:N
 tic;
 ind = nn(ii):nn(ii+1)-1;
 aa_sig_combined(ind) = ...
   sum(diag(inner_freq(1:end-1,2)) * ...
       cos(2 .* pi .* inner_freq(1:end-1,1) * t(ind)) ...
       .+ repmat(inner_freq(1:end-1,3),[1 length(ind)]));
 toc
endfor

which gives
nn =

     1   3921   7841  11760  15680

Elapsed time is 6.94054 seconds.
Elapsed time is 6.94747 seconds.
Elapsed time is 7.04162 seconds.
Elapsed time is 6.94685 seconds.

so a total time of about 28 sec.








reply via email to

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