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!

 채택된 답변

Rik
Rik 2019년 5월 8일

4 개 추천

I'm going to assume your last element of B should be 1 larger, because then your C makes sense. The code below should do what you need by looping over your different time segments.
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];
B= [734139 734141
734145 734146];
row=false(size(A,1),1);
t_=A(:,1);
for k=1:size(B,1)
t1=B(k,1);t2=B(k,2);
row=row | (t_>=t1 & t_<=t2);
end
C=A(row,:);
clc
fprintf('%6d %02d %3.1f\n',C')

댓글 수: 4

Adam Danz
Adam Danz 2019년 5월 8일
편집: Adam Danz 2019년 5월 8일
Similar solution that stores each comparison in a matrix and then combines columns.
d = false(size(A,1),size(B,1));
for i = 1:size(B,1)
d(:,i) = A(:,1)>=B(i,1) & A(:,1)<=B(i,2);
end
finalSelection = A(any(d,2),:);
Thanks for the inspiration Adam. Because of the structure of your code I had a look at implicit expansion again. The code below is your loop, but without a loop:
d=bsxfun(@ge,A(:,1),B(:,1)') & bsxfun(@le,A(:,1),B(:,2)');
C=A(any(d,2),:);
clc,fprintf('%6d %02d %3.1f\n',C')
Adam Danz
Adam Danz 2019년 5월 8일
Nice teamwork! :D
Thank you both! i'll try

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2019년 5월 8일

0 개 추천

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

Adam Danz
Adam Danz 2019년 5월 8일
편집: Adam Danz 2019년 5월 8일
datetime() was released in r2014b. table2timetable() and timerange() in r2016b. OP mentioned that this must work in r2012a.
Hi Peter, thank you very much in fact i'm using those functions in my original code. But how Adams writes, i have to run this in r2012a product.
Got it, I did not catch that. Sorry.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Calendar에 대해 자세히 알아보기

제품

질문:

2019년 5월 8일

댓글:

2019년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by