MATLAB Answers


finding the mean for large data

Asked by FIR
on 30 Nov 2012

I have 3 rows ans 192 column,i want to find mean for each 9 columns in row wise,

for ex if i have rand(3,192)

taking 3x9 , i need mean in row wise so i will have 3x1 matrix after finding mean

please help

for each 3x9 i want to perform


Jos (10584)
on 30 Nov 2012

192 is not divisible by 9, so what to do with the last columns?

It's not "large" either. Before I read it I thought that he was talking hundreds of millions of elements, not five hundred and something. (And there is a "bug" for the case of calculating the mean for large numbers of singles)



No products are associated with this question.

2 Answers

Answer by Andrei Bobrov
on 30 Nov 2012
Edited by Andrei Bobrov
on 30 Nov 2012

A = rand(3,192);
n = 9;
s = size(A);
out = squeeze(nanmean(reshape([A nan(s(1),mod(-s(2),n))],s(1),n,[]),2));


out = blockproc(A,[1 n],@(x)mean(;


k = s(2) - rem(s(2),n);
out = [squeeze(mean( reshape(A(:,1:k),s(1),[]),2 )) , mean(A(:,k+1:end),2)];


Answer by Jan Simon
on 30 Nov 2012

A = rand(3, 192);
B = reshape(A(:, 1:189), 3, 9, []);
C = squeeze(mean(B, 2));


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!