Average of every n values in a matrix.

4 views (last 30 days)
I have mx6 matrix. I want to take average of every 10 values for each column ( avg of 1-10, 11-20, .....). I tried the code below for nx1 vector and it worked. I need a help for that of 6 columns. Thank you.
A = readmatrix('cleanedData.csv');
n = 10;
s = numel(A);
out = nanmean(reshape( [A(:);nan(mod(-s,n),1)],n,[]));
avg_data = transpose(out);

Accepted Answer

Mathieu NOE
Mathieu NOE on 11 Feb 2021
hello
see 2 examples below , without and with overlapping sections
% dummy data
data = rand(320,15);
buffer = 5; % nb of samples for averaging
% zero overlap mean averaging
[m,n] = size(data)
for ci=1:fix(length(data)/ buffer)
start_index = 1+(ci-1)*buffer;
stop_index = min(start_index+ buffer,length(data));
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
avg_data(ci,:) =mean(data(start_index:stop_index,:)); %
end
figure(1),
plot(time_index,avg_data);
% averaging with overlap
shift = 5; % nb of samples for averaging
buffer = 50; % nb of samples for averaging
overlap = buffer-shift
for ci=1:fix((length(data)-buffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ buffer,length(data));
out_data{ci} =data(start_index:stop_index,:); %
figure(ci),
plot(out_data{ci});
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!