How to compute 8 days mean from one year data?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am using the following matlab code
data_matrix=readtable('C:\matlab\sample.csv');
%disp(data_matrix)
[ad,~,cd] = unique(data_matrix,'rows');
out_day = [ad,accumarray(cd,data_matrix,[],@nanmean)];
% Write output as matrix in csv format
filename = 'c:/matlab/sample_2014.csv'
writematrix(out_day,filename);
and my input file is attached as follows
This code is giving error and not computing 8 days average mean.
Please suggest me how to fix it and get the 8 days average mean. The dimension of input file is (365,9) and the dimension of output should be (46,9)
Thanks a lot for your help.
Devendra
0 commentaires
Réponse acceptée
Adam Danz
le 7 Juil 2023
Modifié(e) : Adam Danz
le 7 Juil 2023
Here is a boxcar average of your 365x9 matrix with an averaging window of 8x1 except for the last bin. Averaging is independent for each column producing a 46x9 matrix.
data_matrix=readmatrix('sample.csv');
size(data_matrix)
windowSize = 8; % 8x1
start = 1:windowSize:height(data_matrix);
stop = [start(2:end)-1, height(data_matrix)];
nbins = numel(stop);
dataAverages = nan(nbins, width(data_matrix));
for i = 1:nbins
rows = start(i) : stop(i);
dataAverages(i,:) = mean(data_matrix(rows, :), 1, 'omitnan');
end
size(dataAverages)
0 commentaires
Plus de réponses (3)
Satwik Samayamantry
le 7 Juil 2023
Hi Devendra, Can you please elaborate the problem statement you are working upon? It's not clear what are you trying to achieve.
Nathan Hardenberg
le 7 Juil 2023
data_table = readtable('sample.csv'); % reading as TABLE
data_matrix = table2array(data_table); % convert to matrix
movAvg = movmean(data_matrix, 8, "omitnan"); % moving average with windowsize 8
idx = (1:8:length(movAvg)) + 3 % get indecies where you want to get datapoints
eightDayAverage = movAvg(idx, :) % get the 46 datapoints accordingly
As an advice: it is always good to paste the full error message into your question. Then it is easier to see what's wrong. In your case you did input the table and not a matrix into the accumarray-function. But fixing this did not work either. Maybe someone else is willing to try it with the accumarray-function.
0 commentaires
Torsten
le 7 Juil 2023
Modifié(e) : Torsten
le 7 Juil 2023
data_matrix = readtable('sample.csv');
data_matrix = table2array(data_matrix);
n = floor(365/8);
out_day = zeros(n+1,9);
for i = 1:n
out_day(i,:) = nanmean(data_matrix(1+(i-1)*8:i*8,:));
end
out_day(end,:) = nanmean(data_matrix(1+n*8:end,:));
out_day
0 commentaires
Voir également
Catégories
En savoir plus sur Standard File Formats 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!