How to join data elements with the same date in a table
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Ankriti Sachan
am 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.
0 Kommentare
Peter Perkins
am 2 Mär. 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!