How to join data elements with the same date in a table
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone, I have a table with the following variables: date, station, value where in each row is represented a value for a station for the specific date.
I would to have a time-series of values for each station:
date1 date 2 date 3
station1 value1 value2 ..... value 100...
.
.
station N value 1 value 2 value 100 ....
That is, I would like to collapse all the elements with the same date together, and then differentiate them based on the station name. In particular I would like to have in each row a time series relative to a particular station.
How could I achieve that?
Thank you.
0 个评论
回答(2 个)
Ankriti Sachan
2021-4-21
From the question, it appears that using the Timetables might help you here, though you might get a transpose of the table that you are expecting.
0 个评论
Peter Perkins
2022-3-2
This is a one-liner with unstack:
>> date = datetime(2022,1,[1 1 1 2 2 2 3 3 3])';
>> station = categorical(["A" "B" "C" "A" "B" "C" "A" "B" "C"])';
>> value = rand(9,1);
>> tt = timetable(date,station,value)
tt =
9×2 timetable
date station value
___________ _______ __________________
01-Jan-2022 A 0.568823660872193
01-Jan-2022 B 0.469390641058206
01-Jan-2022 C 0.0119020695012414
02-Jan-2022 A 0.337122644398882
02-Jan-2022 B 0.162182308193243
02-Jan-2022 C 0.794284540683907
03-Jan-2022 A 0.311215042044805
03-Jan-2022 B 0.528533135506213
03-Jan-2022 C 0.165648729499781
>> unstack(tt,"value","station")
ans =
3×3 timetable
date A B C
___________ _________________ _________________ __________________
01-Jan-2022 0.568823660872193 0.469390641058206 0.0119020695012414
02-Jan-2022 0.337122644398882 0.162182308193243 0.794284540683907
03-Jan-2022 0.311215042044805 0.528533135506213 0.165648729499781
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!