How to take an average by date
이전 댓글 표시
I have a set of files, each with data from a trial of an experiment. I need to run a function on each file to summarize the data from each file (in a percentage). I then need to group the files by date (of the data collection) and average the percentages for each day, so that I have one total percentage for each day of data. I load the files using 'dir' and I have the function written to get the percentage in each file, but I cannot figure out how to group the files by date to take the average of each day. I eventually want to be able to plot the data with one point for each day that I have files from. Any help would be appreciated!
답변 (2개)
Peter Perkins
2018년 3월 23일
Put your data in a timetable, then use retime, with 'mean'. It's one line:
>> tt = timetable(datetime(2018,3,23,1:12:71,0,0)',rand(6,1))
tt =
6×1 timetable
Time Var1
____________________ _______
23-Mar-2018 01:00:00 0.95717
23-Mar-2018 13:00:00 0.48538
24-Mar-2018 01:00:00 0.80028
24-Mar-2018 13:00:00 0.14189
25-Mar-2018 01:00:00 0.42176
25-Mar-2018 13:00:00 0.91574
>> ttMean = retime(tt,'daily','mean')
ttMean =
3×1 timetable
Time Var1
____________________ _______
23-Mar-2018 00:00:00 0.72127
24-Mar-2018 00:00:00 0.47108
25-Mar-2018 00:00:00 0.66875
KSSV
2018년 3월 20일
0 개 추천
You have dates in hand.......convert the dates into datevec. From these you can sort them. Read about datevec.
댓글 수: 2
a p
2018년 3월 20일
clc; clear all ;
dates = [ 2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 3 12
2018 3 12
2018 3 12
2018 3 12
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13] ;
%
months = dates(:,2) ;
dayes = dates(:,3) ;
[c,ia,ib] = unique(months) ;
iwant = cell(length(c),1) ;
for i = 1:length(c)
iwant{i} = dates(ib==i,:) ;
iwant{i}
end
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!