How to compute 8 days mean from one year data?
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (3)
Satwik Samayamantry
il 7 Lug 2023
0 voti
Hi Devendra, Can you please elaborate the problem statement you are working upon? It's not clear what are you trying to achieve.
1 Commento
Devendra
il 7 Lug 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.
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
Categorie
Scopri di più su Data Import and Management in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!