how to sort a matrix from one of its vector?

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

Gibus
Posts: 9
Joined: Tue Dec 07, 2010 2:57 am

how to sort a matrix from one of its vector?

Postby Gibus » Tue Dec 14, 2010 6:30 am

Hello

I use eviews7.
I got a matrix(7,2) containing 2 vectors (V1,V2).
I would like to "sort", "rank" or "order" (whatever the command name), by descending order, the matrix using V1 (V2 reorders as well depending on V1 and is not fixed).
Then identify the 2nd max of V1 (logically the 2nd row), and index the name.
Then extract the corresponding 2nd line of V2 to check if the value is bellow a threshold.

I know that Eviews allows group.sort(X1,X2)

but how can I handle it with matrix?

thanks

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: how to sort a matrix from one of its vector?

Postby EViews Gareth » Tue Dec 14, 2010 9:07 am

use the capplyranks function.

Gibus
Posts: 9
Joined: Tue Dec 07, 2010 2:57 am

Re: how to sort a matrix from one of its vector?

Postby Gibus » Tue Dec 14, 2010 10:01 am

Thanks but I don't understant how to use this command.

m1: input matrix (8 lines, 2 columns) with 2 columns named respectively C1 and C2
m2: output ordered matrix

matrix m2 = @capplyranks(m1, C1) 'doesn't work

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: how to sort a matrix from one of its vector?

Postby EViews Glenn » Tue Dec 14, 2010 1:23 pm

This is off the top of my head, but

Code: Select all

vector rks1 = @ranks(@columnextract(m1, 1)) matrix m2 = @rapplyranks(m1, rks1)
should do what you want. The first line extracts the ranks from the first column of M1, and the second applies the ranks to the rows of M1. You should, of course, be able to put this in one long line as in

Code: Select all

matrix m2 = @rapplyranks(m1, @ranks(@columnextract(m1, 1)))
Note also that I'm using @rapplyranks which sorts the rows. You can guess what @capplyranks does :)

thierrydb
Posts: 3
Joined: Thu Apr 07, 2011 12:51 am

Re: how to sort a matrix from one of its vector?

Postby thierrydb » Sun Apr 10, 2011 3:26 pm

When I apply proposed code to a matrix that contains around 30.000 rows, I obtain the following error message: "Invalid permutation index vector".
Is there a size limit for @rapplyranks?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: how to sort a matrix from one of its vector?

Postby EViews Gareth » Mon Apr 11, 2011 8:17 am

I don't believe so - here's one that works for me with a matrix with 40,000 rows:

Code: Select all

create u 100 matrix(40000,4) m1 rnd(m1) vector rks1 = @ranks(@columnextract(m1, 1)) matrix m2 = @capplyranks(m1, rks1)

msz111
Posts: 5
Joined: Mon Mar 07, 2016 7:34 am

Re: how to sort a matrix from one of its vector?

Postby msz111 » Thu Mar 10, 2016 4:57 am

Hi, I am facing a problem very similar to the one described by thierrydb ( a while ago now!),
and it seems to me that the problem when the number of rows is very large is that some rows have the same rank.
Here is an example of the problem in rows 1221 and 1222.
R1217 1217
R1218 1218
R1219 1219
R1220 1220
R1221 1221.5
R1222 1221.5
R1223 1223
R1224 1224
R1225 1225

I guess that when the entries the same the ranking vector gives them equal rank. This unfortunately gets in the way of sorting the matrix. Would be grateful for any suggestions to get around this.

Thank you
Michele

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: how to sort a matrix from one of its vector?

Postby EViews Gareth » Thu Mar 10, 2016 8:05 am



Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests