Average of every n values in a matrix.

8 vues (au cours des 30 derniers jours)
Austin Ukpebor
Austin Ukpebor le 11 Fév 2021
Commenté : Austin Ukpebor le 11 Fév 2021
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);

Réponse acceptée

Mathieu NOE
Mathieu NOE le 11 Fév 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
  1 commentaire
Austin Ukpebor
Austin Ukpebor le 11 Fév 2021
This worked for me. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by