Automatically sorting years and quarters in a serial number

조회 수: 1 (최근 30일)
Wesso
Wesso 2023년 1월 28일
답변: cdawg 2023년 1월 28일
Hi,
I have alarge sample of quarterly data. I have years (example 1990....2022) and quarters (1,2,3,4). How can i generate a serial number that sorts the years and numbers where all firms with year 1990 and quarter 1 will have a value of 1 ; then 1990 quarter 2 will have a value of 2 ....1991,quarter 1 will have a value of 5 etc....
I appreciate your support

채택된 답변

cdawg
cdawg 2023년 1월 28일
If you know the year you want to start with, I think this could work-
years = [1990 1991 1990 1992 1993 1999 1996];
quarters = [3 1 1 2 1 2 4];
startYr = min(years);
serialNo = 4*(years-startYr)+quarters
serialNo = 1×7
3 5 1 10 13 38 28
[srt ind] = sort(serialNo);
data = [srt' years(ind)' quarters(ind)']
data = 7×3
1 1990 1 3 1990 3 5 1991 1 10 1992 2 13 1993 1 28 1996 4 38 1999 2

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 28일
One of the easy solutions is to cretae a table array with variable names: Year (numerical) and Quarter (categorical array), e.g. categorical array, e.g. Q1, Q2, Q3, Q4 per year and then by the Q names, you can sort your data.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by