Average of every n values in a matrix.

5 次查看(过去 30 天)
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);

采纳的回答

Mathieu NOE
Mathieu NOE 2021-2-11
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by