help-octave
[Top][All Lists]
Advanced

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

Re: Taking a mean of a fixed length number of points


From: Augustin Lefèvre
Subject: Re: Taking a mean of a fixed length number of points
Date: Sat, 30 Nov 2019 06:18:44 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

Dear Henk,

You're asking a question about "the best possible way to encode a block average operator in octave".

One possible way you could try to save time is to write "for loops" in an oct-file

It would look like translating the following octave code into C++ :

h=floor(n/k);
y=zeros(h,m);
for ik=1:h
    tstart = (ik-1)*k+1;
    tstop = ik*k;
    y(ik,:)=mean(x(tstart:tstop,:));
end


But I can't guarantee it will be faster : it might depend on the ratio n/k. You will find as attachment an investigation into this matter, which I summarize in the post-scriptum.

If that does not work, you might try encoding your block average operator into a sparse matrix.

Best regards,
Augustin

Post-Scriptum :
"it might depend on the ratio n/k"

My bet would be :
n/k high (high n, small k) => for loop is faster
n/k low (small n, high k) =>  your code is faster

Assuming this, there should be a correlation between n/k and the ratio of (CPU time 1)/(CPU time 2).

To test this correlation write the for loop directly in octave, and if it holds, writing the oct file will actually make the "for loop"  competitive.

...

Ok, so I tried it (see attached script), and the correlation is weak, so either an oct-file will unconditionally save time or not.


On 30/11/2019 02:50, Nicholas Jankowski wrote:
On Fri, Nov 29, 2019 at 4:12 PM Henk Borsje <address@hidden> wrote:

This question is not of crucial importance, but I’m curious if there is a better alternative.

 

I need to calculate the columnar average of each k points out of an array of x(n,m) elements, resulting in a new array  x_avg (floor (n/k) , m) elements.

 

I use:

 

        tmp = reshape(x,k,[],m);                                      (1)

        x_avg = reshape(mean(tmp),[],m);                  (2)

 

I wonder if there is a simpler or more direct way, except for substituting equation (1) into (2)



just for clarity, can you give a brief example of what your input looks like and what you would like the output to look like?  it seems your method is fairly compact. 


    

Attachment: blockavg.m
Description: Text Data


reply via email to

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