# Checking/Counting values in a given Timewindow

3 Ansichten (letzte 30 Tage)
JamJan am 11 Dez. 2018
Bearbeitet: TADA am 11 Dez. 2018
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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Bearbeitet: TADA am 11 Dez. 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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

GT am 11 Dez. 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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by