グループごとにretimeを適用する
15 ビュー (過去 30 日間)
古いコメントを表示
日付とグループ番号が記載された2列のtimetableがあります。日付は2019/4~2020/3のもので、グループごとに欠損している日付が異なります。グループごとに欠損した日付をretimeで埋める方法はないでしょうか。
3 件のコメント
Hiro Yoshino
2021 年 11 月 30 日
例えば
newTimetable = retime(oldTimetable,"regular","linear",...
"TimeStep",milliseconds(2));
とすると、2 ms で 再サンプリングできます。
ここを触って、日や時間で埋めることができます。ご参考まで。
採用された回答
Kojiro Saito
2021 年 11 月 30 日
編集済み: Kojiro Saito
2021 年 11 月 30 日
【編集後】
いただいたコメントでやりたいことが理解できました。
グループ番号毎にtimeが2019/4/1~2020/3/31で、datacountが0の配列(下記のコードでnewTで定義したもの)を作り、変数Tに含まれている日付とグループ番号の場合はTのdatacountを代入する方法です。
下記のコードで実現できると思います。
[g, groupId] = findgroups(T.group);
timeRange = datetime(2019,4,1):days(1):datetime(2020,3,31); % 2019/4/1~2020/3/31の1日毎の要素
% グループ番号毎にtimeが2019/4/1~2020/3/31で、datacountが0のテーブル
newT = table;
newT.time = repmat(timeRange, [1, length(groupId)])';
newT.datacount(:) = 0;
newT.group(:) = sortrows(repmat(groupId', [1, length(timeRange)])');
% 変数Tに含まれている日付とグループ番号の場合は、Tのdatacountを代入
for n=1:height(T)
idx = (T.time(n) == newT.time) & (T.group(n) == newT.group);
newT.datacount(idx) = T.datacount(n);
end
実行してnewTに所望のデータが格納されると思います。
【編集前】
変数Tにtimeとgroupがソートされた状態で格納されているようですので、グループ毎に処理させずにテーブルの全行について、time列で日付が欠損してNaTになっている部分をfillmissingで埋める方法でどうでしょうか。
T.time = fillmissing(T.time, 'nearest');
5 件のコメント
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!