MATLAB: How to count how many x are greater than (x>0.2, 03 and 0.4) in a specific time

count

Hi Everybody,
I need to know how many (x>0.2,0.3&0.4)occurred at each hour. First, count it to make me able tabulate it (i.e. in time between 5-6, we observed 5 event(x>0.2) and so on) and then plot it as a bar. Please assume, x and y are defined as a column data in .xls format.
x y
0.05 10
0.1 10.1 (1 min)
0.02 10.2
0.2 10.3
0.012 10.4
. .
. .
. .
0.2 11
Thank you in advance,

Best Answer

The following could be a solution..
>> x = rand(1,1e3)/2 ; % Fake x, for the example.
>> y = sort(rand(1,1e3)*23.99) ; % Fake y, for the example.
Build a cell array of distributions
>> bins = 0.05 : 0.1 : 0.45 ;
>> dists = arrayfun(@(h) hist(x(floor(y)==h), bins), 0:23, ...
'UniformOutput', false) ;
Test..
>> size(dists)
ans =
1 24
>> dists{1} % Distribution for the period [0-1h[.
ans =
6 6 9 12 6
>> dists{12} % Distribution for the period [11-12h[.
ans =
3 12 4 7 5
so for the period [11-12h[, 3 values are in the range [0,0.1[, 12 in the range [0.1, 0.2[, etc..
Note: you can achieve the same thing using a more basic approach, that you would have to put in a loop..
>> id_hr = y >= 11 & y < 12 ; % Index of y values (hrs) in the range 11-12h.
>> sum(x(id_hr) < 0.1) % Count # of corresponding x values below 0.1.
ans =
3
here we find the 3 that we found above as 1st element of dists{12}.