Suming data over 10 sec period

1 次查看(过去 30 天)
Lily
Lily 2013-9-28
编辑: Jan 2013-9-28
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 个)

Jan
Jan 2013-9-28
编辑:Jan 2013-9-28
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
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
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.

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by