how to find the index of same datetime value

12 ビュー (過去 30 日間)
uzzi
uzzi 2022 年 11 月 7 日
コメント済み: Lei Hou 2022 年 11 月 18 日
I have two tables of different sizes of datetime
t = 1251x1 datetime
v = 115x1 datetime with the format of YYYY-MM-DD hh:mmm:ss.SSS as attached below.
I'd like to compare datetime (hh:mmm:ss) without considering .SSS and find the index of the same compared values. Can someone help me with this?
  1 件のコメント
Stephen23
Stephen23 2022 年 11 月 10 日
編集済み: Stephen23 2022 年 11 月 10 日
"I'd like to compare datetime (hh:mmm:ss) without considering .SSS and find the index of the same compared values."
Then use DATESHIFT and ISMEMBER. No loops are required.
DO NOT convert to text and perform text comparisons in complicated nested loops.

サインインしてコメントする。

採用された回答

Khushboo
Khushboo 2022 年 11 月 7 日
Hello,
You can use datestr to convert the datetime to a string (it handles the .SSS part). I am not sure if there is any existing function that can compare different rows of different tables to find rows with identical value in a column. So this is what I have done, I ma sure more efficient solutions might exist:
%assuming the two tables are A,B with first column having the datetime value
A.t = datestr(A.t);
B.t = datestr(B.t);
h1 = height(A);
h2 = height(B);
for i = 1:h1
for j = 1:h2
if strcmp(A{i,1},B{j,1}) == 1
i,j
break
end
end
end
Hope this helps!
  4 件のコメント
uzzi
uzzi 2022 年 11 月 7 日
Hello Khushboo,
I'll give you more details about it tmr.
Lei Hou
Lei Hou 2022 年 11 月 18 日
dateset is now in discourage use. You can use [h,m,s] = hms(datetime) to extract time components and then call floor(s) to remove .SSS data.

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 11 月 7 日
Truncate the datetime to the previous whole second then compare.
d = datetime('now');
d.Format = d.Format + ".SSSSSS"
d = datetime
07-Nov-2022 13:52:16.687821
d2 = dateshift(d, 'start', 'second')
d2 = datetime
07-Nov-2022 13:52:16.000000
d3 = dateshift(d, 'start', 'minute')
d3 = datetime
07-Nov-2022 13:52:00.000000
  2 件のコメント
uzzi
uzzi 2022 年 11 月 7 日
I am sorry. I don't understand your answer. Can you elaborate more, please?
Steven Lord
Steven Lord 2022 年 11 月 7 日
Let's say I want to check if right now is 6:13 PM (and some number of seconds.) In the example below even though d has those "some number of seconds" d3 trims off those seconds. d is not equal to d4, but d3 is.
d = datetime('now');
d.Format = d.Format + ".SSSSSS"
d = datetime
07-Nov-2022 18:13:15.396629
d2 = dateshift(d, 'start', 'second')
d2 = datetime
07-Nov-2022 18:13:15.000000
d3 = dateshift(d, 'start', 'minute')
d3 = datetime
07-Nov-2022 18:13:00.000000
d4 = datetime(2022, 11, 7, 18, 13, 0)
d4 = datetime
07-Nov-2022 18:13:00
isequal(d4, d3)
ans = logical
1
isequal(d4, d)
ans = logical
0

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by