How to determine values between two points and two times?

8 vues (au cours des 30 derniers jours)
Ayman Alharbi
Ayman Alharbi le 17 Déc 2019
Commenté : dpb le 20 Déc 2019
I want to determine the values between two measurements and between two dates (with 1-day interval) . For example, the dates are (15th to 30th of August 2018) and the measurements are (-9 to -12).
Thanks!
  2 commentaires
dpb
dpb le 17 Déc 2019
See documentation for timeseries object for selecting between dates and logical addressing or indexing for the values. Is two-liner once create the timeseries object.
Ayman Alharbi
Ayman Alharbi le 18 Déc 2019
Thanks. This is a first time using Matlab. Would you please providing an example how I can do that?

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 18 Déc 2019
Modifié(e) : dpb le 18 Déc 2019
<Subscript-into-times-of-timetable> illustrates the time range.
Logical addressing on variables is simply writing the expression wanted..<Logical array indexing>
>> x=randi([-20 0],1,10)
x =
-16 -17 -20 -18 -8 -1 -13 -12 0 -1
>> ix=(x>=-12 & x<=-9)
ix =
1×10 logical array
0 0 0 0 0 0 0 1 0 0
>> x(ix)
ans =
-12
>>
So, if you have a timetable tt, then
it=timerange('2018-08-15','2018-08-30');
ix=(x>=-12 & x<=-9);
tt2=tt.x(it,ix);
Example w/ a made up small data set; you can play with the TMW examples...
>> tt=timetable(datetime(t),rand(size(t))); % just happened to have t hanging around
>> tt.Properties.VariableNames={'x'} % variable name to real variable
tt =
4×1 timetable
Time x
___________ _______
01-Dec-1981 0.66238
03-Jan-1983 0.24417
23-May-1990 0.29551
17-Jun-2012 0.68018
>> it=timerange('01-Jan-1983','31-Dec-1993') % make up a date range vector
it =
timetable timerange subscript:
Select timetable rows with times in the half-open interval:
[01-Jan-1983 00:00:00, 31-Dec-1993 00:00:00)
See Select Timetable Data by Row Time and Variable Type.
>> ix=tt.x>=0.2 & tt.x<=0.3 % now the variable loookup vector
ix =
4×1 logical array
0
1
1
0
>> tt.x(it,ix) % and select the subset wanted...
ans =
0.2442
0.2955
>>
For the logical expression rather than having to write the compound expression I have a little "syntactic sugar" utility routine that lets one write it as single line and makes use in expression or as subscript expression much more legible to read and simpler to write:
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>
Place in an m-file named iswithin.m somewhere on your MATLABPATH and then can write
ix=iswithin(tt.x,0.2,0.3);
or
tt.x(timerange('01-Jan-1983','31-Dec-1993'),iswithin(tt.x,0.2,0.3))
and eliminate the temporary variables if not needed elsewhere.
  2 commentaires
Ayman Alharbi
Ayman Alharbi le 20 Déc 2019
Thank you so much!
dpb
dpb le 20 Déc 2019
If this solves the problem, please ACCEPT Answer to indicate topic closed to others if nothing else...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by