MATLAB: Count number of occurrences in each second.

countMATLAB

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

Best Answer

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)