How to compute 8 days mean from one year data?

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

采纳的回答

Adam Danz
Adam Danz 2023-7-7
编辑:Adam Danz 2023-7-7
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)
ans = 1×2
365 9
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)
ans = 1×2
46 9

更多回答(3 个)

Satwik Samayamantry
Hi Devendra, Can you please elaborate the problem statement you are working upon? It's not clear what are you trying to achieve.
  1 个评论
Devendra
Devendra 2023-7-7
I want to compute the 8days average values for 9 variables from 365 days data. Now I am trying a different matlab code do it but it is also giving error.
data=readtable('C:\matlab\sample.csv');
%disp(data_matrix)
T=table2timetable(data);
TT=retime(T,'8-days mean','mean')
% Write output as matrix in csv format
filename = 'c:/matlab/sample_2014.csv'
writematrix(TT,filename);
The dimension of TT should be (46,9)
I hope it is now clear.
Thanks.
Devendra

请先登录,再进行评论。


Nathan Hardenberg
I would use the moving average function (movmean)
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
idx = 1×46
4 12 20 28 36 44 52 60 68 76 84 92 100 108 116 124 132 140 148 156 164 172 180 188 196 204 212 220 228 236
eightDayAverage = movAvg(idx, :) % get the 46 datapoints accordingly
eightDayAverage = 46×9
0.3548 0.3784 0.5569 0.7060 0.4252 0.4261 0.3547 0.4538 0.5223 0.2562 0.1745 0.5648 0.6264 0.3600 0.3550 0.4437 0.2553 0.6969 0.4398 0.2836 0.6227 0.7054 0.3693 0.3653 0.2884 0.2312 0.7599 0.7363 0.5543 0.7236 0.7493 0.3609 0.3551 0.1303 0.3299 0.7261 0.8566 0.8385 0.6312 0.6574 0.4194 0.4178 0.1812 0.6236 0.4623 0.6179 0.7404 0.6243 0.7155 0.3347 0.3425 0.1551 0.7185 0.3177 0.3560 0.5496 0.6344 0.7289 0.4693 0.4755 0.1498 0.7014 0.2785 0.1275 0.1984 0.4755 0.5150 0.5141 0.5080 0.1481 0.4182 0.5088 0.3164 0.2182 0.4501 0.5799 0.5168 0.5134 0.2561 0.2589 0.7062 0.5180 0.3383 0.6504 0.7485 0.3273 0.3230 0.3196 0.2329 0.7749
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.

Torsten
Torsten 2023-7-7
编辑:Torsten 2023-7-7
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
out_day = 46×9
0.3369 0.3542 0.5598 0.6884 0.4191 0.4187 0.3497 0.4362 0.5358 0.2697 0.1771 0.5582 0.6394 0.3646 0.3599 0.4559 0.2455 0.7094 0.4653 0.3048 0.6491 0.7203 0.3585 0.3548 0.2644 0.2368 0.7599 0.7818 0.6129 0.7220 0.7309 0.3712 0.3657 0.1185 0.3699 0.6964 0.8300 0.8237 0.6206 0.6511 0.4006 0.3996 0.1855 0.6284 0.4519 0.5907 0.7275 0.6364 0.7410 0.3470 0.3549 0.1425 0.7266 0.3039 0.3128 0.5161 0.6099 0.7081 0.4876 0.4931 0.1548 0.6957 0.2749 0.1526 0.1837 0.4502 0.4968 0.5344 0.5279 0.1607 0.3718 0.5599 0.3311 0.2219 0.4811 0.5990 0.4775 0.4741 0.2609 0.2497 0.7184 0.5495 0.3639 0.6476 0.7560 0.3265 0.3223 0.3297 0.2389 0.7758

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by