Suming data over 10 sec period
Show older comments
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?
Answers (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
on 28 Sep 2013
Edited: Azzi Abdelmalek
on 28 Sep 2013
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
on 28 Sep 2013
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.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!