Extract rows from a matrix using time ranges
이전 댓글 표시
Hello everyone, i have a problem. I have to extract rows from a matrix using time ranges (each row between those dates), for example
mi matrix look like,
A=734139 2 1.2
734140 3 1.4
734141 10 0.4
734142 8 0.1
734143 14 2.1
734144 20 4.3
734145 1 5.1
734146 2 3.1
734147 3 2.1
734148 2 1.3
And i have other matrix with time ranges for example (initial date and final date of ranges):
B= 734139 734141
734145 734145
I expect have this
C= 734139 2 1.2
734140 3 1.4
734141 10 0.4
734145 1 5.1
734146 2 3.1
I know how did it with the new time tools (like timetables for example).. in fact is done, but i must use only basic tools because i'm translatting this code for old products (matlab2012a specifically) and octave.
Thank you for your help!
채택된 답변
추가 답변 (1개)
Peter Perkins
2019년 5월 8일
dulio, I strongly recommend that you use datetims and timetables. You will likely be happier in the long run:
>> X = [734139 2 1.2
734140 3 1.4
734141 10 0.4
734142 8 0.1
734143 14 2.1
734144 20 4.3
734145 1 5.1
734146 2 3.1
734147 3 2.1
734148 2 1.3];
>> t = array2table(X,'VariableNames',{'Date' 'X' 'Y'});
>> t.Date = datetime(t.Date,'ConvertFrom','datenum','Format','dd-MMM-yyyy');
>> tt = table2timetable(t)
tt =
10×2 timetable
Date X Y
___________ __ ___
01-Jan-2010 2 1.2
02-Jan-2010 3 1.4
03-Jan-2010 10 0.4
04-Jan-2010 8 0.1
05-Jan-2010 14 2.1
06-Jan-2010 20 4.3
07-Jan-2010 1 5.1
08-Jan-2010 2 3.1
09-Jan-2010 3 2.1
10-Jan-2010 2 1.3
>> dt = datetime([734139 734141],'ConvertFrom','datenum')
dt =
1×2 datetime array
01-Jan-2010 00:00:00 03-Jan-2010 00:00:00
>> tr = timerange(dt(1),dt(2))
tr =
timetable timerange subscript:
Select timetable rows with times in the half-open interval:
[01-Jan-2010 00:00:00, 03-Jan-2010 00:00:00)
See Select Timetable Data by Row Time and Variable Type.
>> tt1 = tt(tr,:)
tt1 =
2×2 timetable
Date X Y
___________ _ ___
01-Jan-2010 2 1.2
02-Jan-2010 3 1.4
댓글 수: 3
datetime() was released in r2014b. table2timetable() and timerange() in r2016b. OP mentioned that this must work in r2012a.
duilio fonseca
2019년 5월 8일
Peter Perkins
2019년 5월 9일
Got it, I did not catch that. Sorry.
카테고리
도움말 센터 및 File Exchange에서 Calendar에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!