How can I delete some specific rows from a matrix?
2 次查看(过去 30 天)
显示 更早的评论
I have an easy question but somehow I am not being able to solve it.
Suppose I have a datetime matrix, A where
'2003-06-24'
'2003-07-10'
'2003-07-18'
'2003-07-26'
'2003-08-03'
'2003-08-11'
'2003-08-19'
'2003-08-27'
And I have another matrix B that contains every day/date from '2003-06-01' to '2003-09-01'.
Can anyone please tell me how can I filter out the rest of the dates from Matrix B so that only the dates from matrix A remain in B?
In another word, how can I make B just like A? (by removing all the unnecessary dates)
Any feedback from you will be greatly appreciated!
0 个评论
采纳的回答
Steven Lord
2022-12-13
Do you have a character matrix or do you have a datetime matrix?
C = ['2003-06-24'
'2003-07-10'
'2003-07-18'
'2003-07-26'
'2003-08-03'
'2003-08-11'
'2003-08-19'
'2003-08-27']
dt = datetime(C)
I'll assume you're using the datetime matrix dt. Let's create B:
B = datetime('2003-06-01'):datetime('2003-09-01');
You can use the set functions like intersect, union, setxor, setdiff, etc. on datetime arrays.
BnotDt = setdiff(B, dt);
whos dt B BnotDt
Element 24 of B is in dt:
B(24)
dt(1)
Element 24 of B is not in BnotDt. [It's not in a different location in BnotDt either, since BnotDt is sorted.]
BnotDt(23:25)
issorted(BnotDt)
2 个评论
Steven Lord
2022-12-14
Use the set functions.
C = ['2003-06-24'
'2003-07-10'
'2003-07-18'
'2003-07-26'
'2003-08-03'
'2003-08-11'
'2003-08-19'
'2003-08-27'];
dt = datetime(C);
B = datetime('2003-06-01'):datetime('2003-09-01');
[isItAMember, whereIsIt] = ismember(dt, B)
更多回答(1 个)
Peter Perkins
2022-12-15
Surely you don't just have a list of dates. Presumably you have data at each date. Use a timetable to store all that.
At that point, it becomes one line:
ttB = ttB(ttA.Time,:)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!