sum of timetables for each months

14 ビュー (過去 30 日間)
Martin
Martin 2019 年 9 月 10 日
コメント済み: Martin 2019 年 9 月 11 日
I got 2 timetables buses and cars like below. Does anyone know how to achieve the last table which sum each months?
buses =
2×2 timetable
Time Buses diesel
________ ________ ________
Aug-2019 5 890
Sep-2019 8 910
cars =
2×2 timetable
Time cars petrol
________ ________ ________
Aug-2019 10 45
Sep-2019 80 50
totalvehicles =
2×2 timetable
Time vehicles fuel
________ ________ ________
Aug-2019 15 1035
Sep-2019 88 960

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 9 月 10 日
Buses = [5;8];
cars = [10;18];
Time = datetime(2019,[8;9],1);
buses = timetable(Time,Buses);
cars = timetable(Time,cars);
T = [cars,buses];
totalvehicles = rowfun(@(x,y)x + y,T,'OutputVariableNames','vehicles');
  2 件のコメント
Martin
Martin 2019 年 9 月 10 日
Thanks, the problem however arise if there are more columns in the tables. I were looking for a more general idea. I update my topic
Martin
Martin 2019 年 9 月 11 日
thanks, Andrei!

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

その他の回答 (1 件)

dpb
dpb 2019 年 9 月 10 日
totalvehicles=retime([buses;cars],'monthly',@sum);
  2 件のコメント
Martin
Martin 2019 年 9 月 10 日
That wont work with different variables names
dpb
dpb 2019 年 9 月 11 日
So, fix the variables names...
bus.Properties.VariableNames={'vehicles','fuel'};
car.Properties.VariableNames={'vehicles','fuel'};
>> retime([bus;car],'monthly',@sum)
ans =
2×2 timetable
Time vehicles fuel
___________ ________ ____
01-Aug-2019 15 935
01-Sep-2019 88 960
>>

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by