フィルターのクリア

How to create a new vector with AM/PM labels from vector of times

2 ビュー (過去 30 日間)
Erik J
Erik J 2017 年 3 月 23 日
編集済み: dpb 2017 年 3 月 28 日
I have a single column vector with a bunch of times (in military time). The format of these is 12:00.
I need to create a new vector corresponding to this which is just a 1 if the time is PM (>12:00) or a 0 if the time is AM (<12:00).
I cannot seem to figure out a quick and simple way to do this.
Thank you.

回答 (1 件)

dpb
dpb 2017 年 3 月 23 日
編集済み: dpb 2017 年 3 月 28 日
Still learning way around the datetime object; may be more direct route...
dtm=datetime(num2str([0:23].','%02d:00'),'inputformat','HH:mm','format','a'); % sample vector
isPM=cellfun(@isempty,strfind(cellstr(dtm),'AM')); % check if AM/PM
ADDENDUM
More better--
isPM=(hour(dtm)>=12);
doesn't rely on the formatting nor need costly cell operations.
  2 件のコメント
Peter Perkins
Peter Perkins 2017 年 3 月 27 日
for AM PM, that'd be the way to go, more generally,
timeofday(dtm) > (hours(14) + minutes(30)) % after 2:30 pm, before midnight
dpb
dpb 2017 年 3 月 27 日
編集済み: dpb 2017 年 3 月 28 日
Thanks, Peter, I still find doc dense here per our previous conversation...but I did see a mismatched closing parens above so fixing that is good. :)

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

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by