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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by