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

조회 수: 2 (최근 30일)
Alberto Natali
Alberto Natali 2021년 4월 16일
답변: Peter Perkins 2022년 3월 2일
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일
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일
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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by