필터 지우기
필터 지우기

Extract rows from matrix based on date

조회 수: 11 (최근 30일)
David du Preez
David du Preez 2017년 5월 9일
답변: Peter Perkins 2017년 5월 9일
I have a 88416 x 11 matrix. Hourly date and time datenum values are in column 1. The hourly date range is from 2006-12-1 to 2016-12-31. If the date is 2007-10-28 I want to remove it from the matrix and create a new matrix that contains it and all the values in that row. I want to repeat this for several dates. How do I do it?

채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 9일
If you have R2016b or later, use a timetable() object, and extract rows using a timerange; https://www.mathworks.com/help/matlab/matlab_prog/subscript-into-times-of-timetable.html#zmw57dd0e28127
  댓글 수: 4
David du Preez
David du Preez 2017년 5월 9일
Awesome! thank you.
How would it work for a range of dates from 2007-10-28 to 2007-11-10?
Walter Roberson
Walter Roberson 2017년 5월 9일
fd = floor(YourArray(:,1));
match = fd >= datenum([2007,10,28]) & fd <= datenum([2007,11,10]);
extracted_data = YourArray(match,:);
Alternately,
match = YourArray(:,1) >= datenum([2007,10,28]) & YourArray(:,1) < (datenum([2007,11,10]) + 1);
extracted_data = YourArray(match, :);
Note that the first operation is >= but the second operation is strictly < comparing to one day after your last day. This is because datenum are in whole days and fractions of a day, so everything up to .999999999 (etc) of the ending day belongs to the ending day, as soon as you get to the whole number next day the date stops belonging to the range.
Note: this end date calculation is not guaranteed to be valid for the days that have leap seconds. datenum format is quite weak in the handling of leap seconds.

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

추가 답변 (1개)

Peter Perkins
Peter Perkins 2017년 5월 9일
David, even prior to R2016b, you might look at using datetimes rather than datenums:
>> d = datetime(2017,10,26,(0:60:420)',0,0)
d =
8×1 datetime array
26-Oct-2017 00:00:00
28-Oct-2017 12:00:00
31-Oct-2017 00:00:00
02-Nov-2017 12:00:00
05-Nov-2017 00:00:00
07-Nov-2017 12:00:00
10-Nov-2017 00:00:00
12-Nov-2017 12:00:00
>> ( '28-Oct-2017'<= d & d<'11-Nov-2017' )'
ans =
1×8 logical array
0 1 1 1 1 1 1 0
Put d and the rest of your data in a table, and although you don't get everything that timetables provide, you might find it easier to subset your data.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by