Count number of occurrences in each second.

10 views (last 30 days)
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
KSSV
KSSV on 5 Jun 2020
Not clear..you need expalin more about your data.
Nick Storr
Nick Storr on 5 Jun 2020
Ok. My data is essentially when beta brainwaves occur. So the trials took between 20 minutes and 30 minutes. When the beta brain wave band is reached it will say it took 9.6 seconds from the start of the trial.
It will then give me an array of times that beta was met, creating a vector. I want to know how many times beta was reached in each 'second' in that vector. Whilst the number represents seconds it is essentially a just a number and doesn't doesn't include time stamps or any other information which needs transforming.
So if beta was present at 1.2 and 1.4 it would show that there were 2 numbers present between 1 and 2.
Does this help? Happy to provide more information.
Many Thanks
Nick

Sign in to comment.

Accepted Answer

madhan ravi
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)

Tags

Products

Community Treasure Hunt

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

Start Hunting!