Suming data over 10 sec period
이전 댓글 표시
Hi
I'm trying to sum my findings over 10 sec period of time. My test data is:
C = [2 3 5 1 0 9 8 7 3 0 2 3 8 0 8]; %data
t = [0.04 1 3 6.4 10 12 19.9 22 26.2 28 30 33.3 35 37 40]; %time
Meaning that my new_C would be:
new_C = [11 17 12 19];
Could you please help me?
답변 (3개)
This is the main job of accumarray:
C = [2 3 5 1 0 9 8 7 3 0 2 3 8 0 8]; %data
t = [0.04 1 3 6.4 10 12 19.9 22 26.2 28 30 33.3 35 37 39]; %time
new_C = accumarray(ceil(t / 10).', C);
Azzi Abdelmalek
2013년 9월 28일
편집: Azzi Abdelmalek
2013년 9월 28일
C = [2 3 5 1 0 9 8 7 3 0 2 3 8 0 8]; %data
t = [0.04 1 3 6.4 10 12 19.9 22 26.2 28 30 33.3 35 37 39]; %time
id=[0 10:10:ceil(max(t)/10)*10]
out=arrayfun(@(x) sum(C(t>id(x)& t<=id(x+1))),1:numel(id)-1)
Image Analyst
2013년 9월 28일
Here's one intuitive, easy to understand, easy to follow way that works:
C = [2 3 5 1 0 9 8 7 3 0 2 3 8 0 8]; %data
t = [0.04 1 3 6.4 10 12 19.9 22 26.2 28 30 33.3 35 37 40];
% Find indexes where we are at a multiple of 10
for k = 1 : length(t)
edges(k) = find(t <= 10*k, 1, 'last');
end
indexes = [0, unique(edges)]
% Sum C up in the intervals.
for k = 2:length(indexes)
theSumOfC(k-1) = sum(C((indexes(k-1)+1):indexes(k)));
end
% Print to command line
theSumOfC
though if you wait long enough, someone will come up with a compact cryptic one-liner.
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!