oldflatop
oldflatop

Reputation: 39

how to avoid a loop in a calculation of mean in matlab

I have a matrix A with size 100000x128. For each row of this matrix I need to calculate the mean of columns 1:16, 17:32... 98:113. The way I'm doing it is with a for loop like this:

    n = 8; % number of windows
    win_size = 128/n;

    for i = 1 : length(A)
     PAA(i,:) = [mean(reshape(A(i,:),win_size,n))];
    end

Any chance I could improve this without the loop? Thanks for the help.

Upvotes: 0

Views: 72

Answers (2)

Daniel
Daniel

Reputation: 36710

You may use reshape, to create another dimension for your blocks:

y=reshape(x,size(x,1),win_size,size(x,2)/win_size)

To get the mean per block, use mean(y,2)

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 47392

>> A = randn(100000, 128);
>> Amean = reshape(mean(reshape(A',16,100000)), 8, 100000)';

Upvotes: 0

Related Questions