How to compute 8 days mean from one year data?
조회 수: 5 (최근 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
댓글 수: 0
채택된 답변
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)
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
추가 답변 (3개)
Satwik Samayamantry
2023년 7월 7일
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
2023년 7월 7일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!