How to compute 8 days mean from one year data?
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Adam Danz
on 7 Jul 2023
Edited: Adam Danz
on 7 Jul 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 Comments
More Answers (3)
Satwik Samayamantry
on 7 Jul 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
on 7 Jul 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 Comments
Torsten
on 7 Jul 2023
Edited: Torsten
on 7 Jul 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 Comments
See Also
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!