Checking/Counting values in a given Timewindow

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by