How to sort matrix (Panel dataset) according to the ID and year index in MatLab?

Dear All,
I am working with firm-level panel dataset in MatLab and I would like to sort my datasets. For instance:
Could you please help me? Many thanks!

 Accepted Answer

It's simply sortrows:
m = [1 1999 22
2 2001 12
1 2001 23
3 2002 13
1 2000 45
2 2000 24
3 2001 15];
msorted = sortrows(m)

2 Comments

Thanks! and that is strange, I have tried this code many times last night, but it just did not work as what I expected. But this time, it works =.= so strange, is that because the MatLab will only sort the columns with clear tendency or regulations, e.g. columns one and two in my case ? while the columns without clear tendency in integer, e.g. column three in my case, will be automatically ignored by MatLab in sortrows? thanks
No, sortrows does not ignore anything. Unless you tell it otherwise (2nd argument), it sorts against the first column. if some elements are equal in the first row it sorts these along the second column and so on. What's in the matrix is irrelevant.
Either you told sortrow to ignore the third column with:
msorted = sortrows(m, [1 2])
or you did something wrong (maybe you used sort instead of sortrows)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Dec 2014

Commented:

on 11 Dec 2014

Community Treasure Hunt

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

Start Hunting!