Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Sorting matrix on a column


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Sorting matrix on a column
Date   Fri, 21 Jan 2011 17:37:02 +0000

I had a quick look through various add-ons that were published by user-programmers about a decade ago and could not spot anything tailor-made. 

So my guess is that the price of an all-Stata solution is that you need to write your own Stata program to do this. 

The alternative is one line long 

mata : st_matrix("a", sort(st_matrix("a"), 1))

where of course "a" should be replaced by your own matrix name in " ". 

I really recommend that you get into Mata. Even a little goes a very long way. 

Nick 
[email protected] 

Bert Jung

I need to sort a matrix according to a specific column.  For example:

mat a = (  ( 4 \ 2 \ 3 \ 1 ) , ( 90 \ 95 \ 92 \ 96 )   )
mat list a

a[4,2]
    c1  c2
r1   4  90
r2   2  95
r3   3  92
r4   1  96


I would like to sort this matrix on the first column in decreasing order to get:

1	96
2	95
3	92
4	90

I realize that Mata's -sort(,)- may do this easily but don't have much
experience with Mata and prefer a "regular" Stata solution.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index