How can I delete some specific rows from a matrix?

조회 수: 2 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 13일
답변: Peter Perkins 2022년 12월 15일
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!

채택된 답변

Steven Lord
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']
C = 8×10 char array
'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)
dt = 8×1 datetime array
24-Jun-2003 10-Jul-2003 18-Jul-2003 26-Jul-2003 03-Aug-2003 11-Aug-2003 19-Aug-2003 27-Aug-2003
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
Name Size Bytes Class Attributes B 1x93 744 datetime BnotDt 1x85 680 datetime dt 8x1 64 datetime
Element 24 of B is in dt:
B(24)
ans = datetime
24-Jun-2003
dt(1)
ans = datetime
24-Jun-2003
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)
ans = 1×3 datetime array
23-Jun-2003 25-Jun-2003 26-Jun-2003
issorted(BnotDt)
ans = logical
1
  댓글 수: 2
Ashfaq Ahmed
Ashfaq Ahmed 2022년 12월 13일
편집: Ashfaq Ahmed 2022년 12월 14일
Hi!
Thank you so much!
While this is a very intersting approach to solve the problem, my question is how do I know that the 24th element of B is in dt? Because I just posted an example case. In my original problem, there are more than 10,000 rows from 2001 to 2021. Can you kindly give me an idea on how to automate this finding?
Thank you again!
Steven Lord
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)
isItAMember = 8×1 logical array
1 1 1 1 1 1 1 1
whereIsIt = 8×1
24 40 48 56 64 72 80 88

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

추가 답변 (1개)

Peter Perkins
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,:)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by