Azzera filtri
Azzera filtri

Suming data over 10 sec period

2 visualizzazioni (ultimi 30 giorni)
Lily
Lily il 28 Set 2013
Modificato: Jan il 28 Set 2013
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?

Risposte (3)

Jan
Jan il 28 Set 2013
Modificato: Jan il 28 Set 2013
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 il 28 Set 2013
Modificato: Azzi Abdelmalek il 28 Set 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
Image Analyst il 28 Set 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.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by