Count number of occurrences in each second.
10 views (last 30 days)
Show older comments
I'm trying to create a script which counts the number of occurrences of an event each second.
For example I receive data such as below and this is when something has been triggered in seconds.
1.1, 1.2, 1.5, 1.9, 2, 2.5, 2.9.
I want to know what code would tell me how many times the event occurred between 1-2 seconds, 2-3 seconds etc. There are potentially around 2000 seconds to do this for. So for 1-2 seconds I would expect to see a count of 4. For 2-3 seconds I would expect to see a count of 3.
Many Thanks
4 Comments
Accepted Answer
madhan ravi
on 5 Jun 2020
Edited: madhan ravi
on 5 Jun 2020
x = [1.1, 1.2, 1.5, 1.9, 2, 2.5, 2.9];
u = unique(fix(x));
Counts = arrayfun(@(y) nnz(x>=y & x<(y+1)), u)
%or
Counts = sum((x>=u.') & (x<(u.'+1)),2)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!