How to join data elements with the same date in a table

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.

回答 (2 件)

Ankriti Sachan
Ankriti Sachan 2021 年 4 月 21 日

0 投票

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.
Please refer to this link to know more about creating a timetable using an example.
Peter Perkins
Peter Perkins 2022 年 3 月 2 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2021 年 4 月 16 日

回答済み:

2022 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by