Checking/Counting values in a given Timewindow
1 view (last 30 days)
Show older comments
In the following Matrix I have the following '1' states according to their positions:
1 1174 1175 2006 2007 2809 2810 3670 3671 4541 4542 5265 5266 6139 6140 6584 6585 6856 6857 7646 7647 8382 8383 9142 9143
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
However, I have certain Timewindows for example:
1280,5 2560,5 3840,5
Now I want to check how many 1's are within this Time Window (1280,5 - 3840,5)
How can I do this?
0 Comments
Accepted Answer
TADA
on 11 Dec 2018
Edited: TADA
on 11 Dec 2018
I'm not sure what the ",5" stand for in your time frame, but you can do something like that:
states = [1 1174 1175 2006 2007 2809 2810 3670 3671 4541 4542 5265 5266 6139 6140 6584 6585 6856 6857 7646 7647 8382 8383 9142 9143;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
% define time window
tw = [1280, 3840];
% if the '1' states are either 1 or 0 you can sum them up like that:
numOnes = sum(states(2, states(1,:) >= tw(1) & states(1,:) <= tw(2)))
numOnes =
6
% otherwise, you can count the ones more specifically:
numOnes = sum(states(2, states(1,:) >= tw(1) & states(1,:) <= tw(2)) == 1)
numOnes =
6
0 Comments
More Answers (1)
GT
on 11 Dec 2018
Hi JamJan,
If I understand correctly you have a matrix with 2 rows, the first row shows times, and the second 1. You are asking to count how many ones between a certain time range.
Here is an example on how to do this: (where I am using larger than 6, and smaller than 23)
a = [ 1:4:40; ones(1,10)]
idx = a(1,:)>6 & a(1,:)<23
sum(a(2,idx))
This uses logical indexing.
I hope this makes sense to you and you can apply this to your case.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!