Automatically sorting years and quarters in a serial number

1 次查看(过去 30 天)
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
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