フィルターのクリア

Looping with datetime greater and less than 24 hour

12 ビュー (過去 30 日間)
eko supriyadi
eko supriyadi 2022 年 6 月 3 日
回答済み: Stephen23 2022 年 6 月 4 日
Hi community,
i'm stuck with looping and datetime
suppose, i have datetime array (actualiy i have large array):
a=[duration(0,22,0);duration(0,52,0);duration(24,06,0);duration(0,1,0);duration(-24,-5,0)];
so, it will produce
a =
5×1 duration array
00:22:00
00:52:00
24:06:00
00:01:00
-24:05:00
Now, i want to subtract those array that are greater than 24 hours by 24 hours and add times that are less than 24 hours by 24 hours..
the result what i want is like this:
a =
5×1 duration array
00:22:00
00:52:00
00:06:00
00:01:00
-00:05:00
this is the construct loop i have done but it still fails:
a=[duration(0,22,0);duration(0,52,0);duration(24,06,0);duration(0,1,0);duration(-24,-5,0)];
a1=[];
for i=1:length(a)
if a(i) >= hours(24);
a1 = a(i)-hours(24);
elseif a(i) < hours(-24);
a1 = a(i)+hours(-24);
else a1 == a;
end;end
appreciated much help!

採用された回答

Steven Lord
Steven Lord 2022 年 6 月 3 日
If the duration is less than -24 hours you want to add 24 hours to it not add -24 hours, right? Also FYI: you can pass vectors into the duration function to create a vector of durations.
a= duration([0; 0; 24; 0; -24], [22; 52; 6; 1; -5], [0; 0; 0; 0; 0])
a = 5×1 duration array
00:22:00 00:52:00 24:06:00 00:01:00 -24:05:00
a(a > hours( 24)) = a(a > hours( 24)) - hours(24)
a = 5×1 duration array
00:22:00 00:52:00 00:06:00 00:01:00 -24:05:00
a(a < hours(-24)) = a(a < hours(-24)) + hours(24)
a = 5×1 duration array
00:22:00 00:52:00 00:06:00 00:01:00 -00:05:00

その他の回答 (4 件)

Walter Roberson
Walter Roberson 2022 年 6 月 3 日
編集済み: Walter Roberson 2022 年 6 月 3 日
h24 = hours(24);
a = mod(a, h24);
mod() works for duration!
  2 件のコメント
dpb
dpb 2022 年 6 月 3 日
Clever; hadn't thought of it.
I was going to post the more-or-less brute force w/o explicit loop of
ix=abs(hours(a))>=24;
a(ix)-hours(24*sign(hours(a(ix))))
ans =
2×1 duration array
00:06:00
-00:05:00
Steven Lord
Steven Lord 2022 年 6 月 3 日
Instead of mod you want rem.
a= duration([0; 0; 24; 0; -24], [22; 52; 6; 1; -5], [0; 0; 0; 0; 0])
a = 5×1 duration array
00:22:00 00:52:00 24:06:00 00:01:00 -24:05:00
aMod = mod(a, hours(24))
aMod = 5×1 duration array
00:22:00 00:52:00 00:06:00 00:01:00 23:55:00
aRem = rem(a, hours(24))
aRem = 5×1 duration array
00:22:00 00:52:00 00:06:00 00:01:00 -00:05:00

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


eko supriyadi
eko supriyadi 2022 年 6 月 3 日
編集済み: eko supriyadi 2022 年 6 月 3 日
and when we interest with <24 hours, we can do indexing one more again:
h24 = hours(24);
mask = a>h24;
a(mask) = a(mask) - h24;
nh24 = hours(-24);
nmask = a<nh24;
a(nmask) = a(nmask) + nh24
and thank you, but I'm still curious about the looping step
Edit mod:
no, mod function still give error result for -24 hour but work for 24 hour
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 6 月 3 日
mod(-hours(24),hours(24)) works for me and gives me duration of 0 hours
dpb
dpb 2022 年 6 月 3 日
What about the looping step?
Other than
a1 = a(i)+hours(-24);
is turning -24 into -48 instead of 0 because of sign, it works.
One doesn't need the else if would just assign a1=a first, then operate on a1 inside the loop.
I didn't test Walters mod() but he rarely makes a goof -- far less frequent than I do, anyways -- I posted a way that works w/o the looping construct.

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


dpb
dpb 2022 年 6 月 3 日
編集済み: dpb 2022 年 6 月 3 日
Since indeed mod() doesn't do what is wanted here for negative hours, I'll go ahead and post the alternative way that's akin to @Steven Lord's --
ix=abs(hours(a))>=24;
a(ix)-hours(24*sign(hours(a(ix))))
ans =
2×1 duration array
00:06:00
-00:05:00
that uses sign to perform the logical test and make correction in proper direction.

Stephen23
Stephen23 2022 年 6 月 4 日
The simple and efficient approach is to use REM:
a = duration([0; 0; 24; 0; -24], [22; 52; 6; 1; -5], [0; 0; 0; 0; 0])
a = 5×1 duration array
00:22:00 00:52:00 24:06:00 00:01:00 -24:05:00
b = rem(a,hours(24))
b = 5×1 duration array
00:22:00 00:52:00 00:06:00 00:01:00 -00:05:00

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by