Automatically sorting years and quarters in a serial number

1 visualización (últimos 30 días)
Wesso
Wesso el 28 de En. de 2023
Respondida: cdawg el 28 de En. de 2023
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

Respuesta aceptada

cdawg
cdawg el 28 de En. de 2023
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

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 28 de En. de 2023
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.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by