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

2 visualizzazioni (ultimi 30 giorni)
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.

Risposte (2)

Ankriti Sachan
Ankriti Sachan il 21 Apr 2021
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 il 2 Mar 2022
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

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Tag

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by