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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by