Checking/Counting values in a given Timewindow

2 ビュー (過去 30 日間)
JamJan
JamJan 2018 年 12 月 11 日
編集済み: TADA 2018 年 12 月 11 日
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?

採用された回答

TADA
TADA 2018 年 12 月 11 日
編集済み: TADA 2018 年 12 月 11 日
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

その他の回答 (1 件)

Gareth
Gareth 2018 年 12 月 11 日
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.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by