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

3 views (last 30 days)
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,
  6 Comments
Ara
Ara on 18 Mar 2013
@ Walter: Exactly. As you said, It should be like 10.01 and 10.10. And the signal varied usually during an hour. It means between each minutes or even more minutes hard to find (x>0.2,0.3 or 0.4).
Image Analyst
Image Analyst on 18 Mar 2013
You'll have to read them in as text then and parse them into two separate numbers.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 18 Mar 2013
Edited: Cedric on 18 Mar 2013
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}.
  6 Comments
Ara
Ara on 18 Mar 2013
Thank you VERY much. Let me play around with the plot and if you do not mind I will ask any question that I faced with.
Ara
Ara on 27 Sep 2020
Edited: Ara on 27 Sep 2020
Hi Cedric,
You wrote a FOR loop in my code. How to get rid of the loop and read data for one day of interest only without going to a circles again and again.
  • I would like to write my CV in Latex. Can you help me or do you have a template of it so that I can use it?
If I find the answer of this question, I will be the happiest woman in this World.
Please tell me how to correct it?
Best,
Ara

Sign in to comment.

More Answers (1)

Cedric
Cedric on 18 Mar 2013
You're welcome!
Well, at this point you should build a test dataset just to check, e.g.
>> s4 = [0.21, 0.21, 0.21, 0.32, 0.45, 0.45, 0.25] ;
>> time = [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2] ;
so there should be [3, 1, 2] associated with period [1-2h[, and [1, 0, 0] associated with period [2-3h[.
>> myHist = @(v) [sum(v>0.2 & v<=0.3), sum(v>0.3 & v<=0.4), sum(v>0.4)] ;
>> dists = arrayfun(@(h) myHist(s4(floor(time)==h)), 0:23, ...
'UniformOutput', false) ;
>> dists{1} % [0-1h[
ans =
0 0 0
>> dists{2} % [1-2h[
ans =
3 1 2
>> dists{3} % [2-3h[
ans =
1 0 0
It seems to be working. Then, using your dataset, you can check manually what happens with the period 5-6h. I show you here with the period 1-2h:
>> id_time = time >= 1 & time < 2 % Flag relevant times.
id_time =
1 1 1 1 1 1 0
this is a vector of logicals that we use then for indexing s4
>> s4_selection = s4(id_time)
s4_selection =
0.2100 0.2100 0.2100 0.3200 0.4500 0.4500
as you can see, these are the elements of s4 that correspond to a time in the range 1-2h[. Now we test myHist on this set of values
>> myHist(s4_selection)
ans =
3 1 2
It seems to be working.. let's check manually that the first count is correct
>> s4_selection > 0.2 & s4_selection <= 0.3
ans =
1 1 1 0 0 0
again, vector of logicals flagging relevant elements; counting them just means summing the vector of logicals (there is a conversion to numeric)
>> sum(s4_selection > 0.2 & s4_selection <= 0.3)
ans =
3
So it's working. Now ARRAYFUN repeats the same kind of operations with the small difference that instead of checking whether time is greater than h and smaller than h+1 for h=0:23, we test whether floor(time)==h.
Hope it helps; let me know if you are having troubles using this material!
  15 Comments
Ara
Ara on 22 Sep 2020
Edited: Ara on 27 Sep 2020
Hi Dr. Cedric Wannaz,
Thank you for providing useful explanation and the best solution back in 2013.
I still read through your code, programming and all you tought me about MATLAB and it is very useful so thanks again for being helpful all the time. However, I wish to contact you as I amfacing some questions about MATLAB programming and I need to send it to your email address. I wrote to your email address as you previuosly provided for me but I guess you no longer check your email!!!
I need to install Mozilla File the one that you have asked me to use in Australia. How can I install that file again?
I have some more questions and I wish to email it to you to discuss more details about MATLAB solution and I need to send the code to you but I could not find your emal address in your profile. Would you mind to provide your email address in your profile or here so that I can send my questions along with my code to you? You have my email address so you can write back to me in any of my email.
Thanks.
Ara

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!