[Gretl-users] matrix indexes question after sort

Franck Nadaud nadaud at centre-cired.fr
Thu Apr 24 11:51:48 EDT 2008


Hi all, greetings from Paris !

I am still developping my spatial tools under gretl, but I have a difficulty
with some matrix functions. My code is an adaptation of the "brute force"
find_neighbor of James LeSage.

Let me explain, I am given a list of coordinates for my data (spatial X and Y
location coordinates). The function is to find the k nearest neighbors of each
of my N data points.

The function computes for each of them a distance to all the others, then i
call a sort command to order the distances from smallest to largest to finally
extract the k smallest distances i am interested in (starting from line 2 to
k+1 because the lowest distance is zero, ie: the shortest distance to data i
is itself !).

Well, my problem is that I do not see how to grab not the values of sorted
distances but the indexes of data points. Here is the code :


matrix xc = X_COORD
matrix yc = Y_COORD
scalar n = rows(xc)

### i fix the number of near neighbors to 4, a typical value (rook neighborhood)

scalar m = 4

### I create a list of indexes of each of the 4 nearest neighbors set to zero

matrix nnlist = zeros(n,m)

### I added this dirty trick to keep track of the indexes I want to sort with
### the distances:

matrix idx = transp(seq(1,n))

### the main (brute force) loop

loop i = 1..n --quiet
    matrix xi = xc[$i,1]
    matrix yi = yc[$i,1]
    matrix dist = sqrt((xc-xi*ones(n,1)).^2 + (yc - yi*ones(n,1)).^2)~idx
    matrix xind = sort(dist[,1])
    matrix nnlist[i,1:m] = transp(xind[2:m+1,1])
end loop

### I display the results to see the results
dist
xind
nnlist

As you can see, I compute a matrix dist holding distances concatenated with
indexes. Then i sort the distances and store them into the matrix xind. The
problem is that, I am not interested by the distances, but the indexes of the
units but xind only returns a vector of sorted distances and not the indexes.
In fact i would be interested in just extracting the values of indexes in the
sorted dist matrix. I do not understand how to use the command sort for
matrices.

any advice ?

thanx & cheers

Franck

PS: a text file with my (X,Y) data

-- 
Franck Nadaud
Economiste
CIRED
UMR 8568 CNRS - EHESS
45 bis avenue de la Belle Gabrielle
94736 Nogent-sur-Marne Cedex
TEL 33-1-43-94-73-94
FAX: 33-1-43-94-73-70
MOB: 06-07-39-92-75
France


-------------- next part --------------
CODAMC	X_COORD	Y_COORD
11 0020	-63	-11
12 0020	-73	-9
12 0030	-71	-9
12 0040	-69	-10
12 0060	-72	-9
13 0020	-72	-6
13 0040	-64	0
13 0070	-68	-9
13 0090	-64	-8
13 0100	-67	-6
13 0120	-64	-4
13 0140	-70	-7
13 0150	-70	-8
13 0160	-66	-2
13 0170	-62	-7
13 0180	-72	-7
13 0210	-68	-2
13 0220	-66	-3
13 0230	-68	-5
13 0240	-66	-8
13 0260	-60	-4
13 0270	-62	-7
13 0280	-65	-3
13 0290	-58	-5
13 0300	-58	-1
13 0310	-59	-4
13 0340	-57	-3
13 0350	-68	-8
13 0360	-66	0
13 0380	-68	0
13 0390	-69	-4
13 0420	-66	-4
14 0010	-61	3
15 0010	-49	-2
15 0020	-49	-2
15 0030	-51	0
15 0040	-55	0
15 0050	-54	0
15 0070	-50	-1
15 0080	-48	-1
15 0090	-46	-1
15 0100	-56	-4
15 0110	-50	-2
15 0120	-50	-3
15 0130	-49	-2
15 0140	-48	-1
15 0150	-48	-1
15 0160	-47	-1
15 0170	-47	-1
15 0180	-51	-1
15 0190	-48	-2
15 0200	-49	-1
15 0210	-49	-2
15 0220	-47	-1
15 0230	-47	-2
15 0240	-48	-1
15 0250	-50	0
15 0260	-48	-1
15 0270	-50	-8
15 0280	-50	-2
15 0290	-48	-1
15 0310	-52	-1
15 0320	-48	-1
15 0330	-49	-2
15 0340	-48	-1
15 0350	-47	-2
15 0360	-56	-6
15 0390	-56	-2
15 0400	-50	-2
15 0410	-48	-1
15 0420	-50	-6
15 0430	-47	-1
15 0440	-48	-1
15 0450	-51	-2
15 0460	-49	-3
15 0480	-54	-1
15 0490	-49	-1
15 0500	-47	-1
15 0510	-56	0
15 0520	-50	-2
15 0530	-57	1
15 0540	-47	-2
15 0550	-48	-3
15 0560	-47	-1
15 0570	-49	-1
15 0610	-47	-1
15 0620	-47	-1
15 0630	-49	-1
15 0640	-49	-1
15 0650	-48	-1
15 0660	-48	-1
15 0670	-50	-9
15 0680	-55	-4
15 0690	-47	-1
15 0700	-48	-1
15 0710	-48	-1
15 0720	-52	-5
15 0730	-52	-7
15 0740	-48	-1
15 0750	-49	-5
15 0760	-48	-2
15 0770	-50	-1
15 0790	-49	0
15 0800	-48	-3
15 0820	-48	-1
16 0020	-51	2
16 0030	-52	1
16 0040	-53	1
16 0050	-52	3
17 0040	-47	-11
17 0100	-48	-6
17 0190	-49	-9
17 0200	-50	-13
17 0230	-49	-8
17 0240	-47	-13
17 0270	-46	-13
17 0290	-48	-6
17 0550	-49	-8
17 0600	-49	-8
17 0700	-47	-12
17 0720	-49	-9
17 0770	-48	-8
17 1050	-47	-8
17 1110	-49	-9
17 1240	-47	-10
17 1320	-49	-10
17 1330	-49	-9
17 1360	-48	-11
17 1420	-48	-12
17 1510	-47	-10
17 1610	-49	-10
17 1620	-48	-13
17 1650	-48	-9
17 1660	-49	-12
17 1670	-49	-9
17 1750	-50	-10
17 1780	-47	-12
17 1790	-47	-11
17 1840	-48	-8
17 2080	-48	-6
17 2090	-47	-12
17 2100	-49	-11
17 2210	-48	-7
21 0020	-45	-2
21 0040	-45	-4
21 0050	-46	-10
21 0060	-47	-6
21 0070	-45	-3
21 0100	-45	-4
21 0110	-44	-3
21 0130	-45	-1
21 0140	-46	-8
21 0160	-45	-6
21 0190	-45	-2
21 0200	-46	-4
21 0230	-44	-6
21 0240	-45	-3
21 0250	-45	-3
21 0260	-46	-2
21 0280	-47	-7
21 0290	-46	-2
21 0300	-43	-5
21 0310	-45	-2
21 0330	-44	-4
21 0350	-44	-6
21 0370	-45	-2
21 0380	-45	-5
21 0400	-45	-5
21 0410	-46	-7
21 0420	-44	-6
21 0440	-44	-5
21 0450	-44	-5
21 0460	-44	-5
21 0470	-44	-5
21 0480	-46	-5
21 0490	-45	-2
21 0510	-44	-3
21 0520	-45	-5
21 0530	-47	-5
21 0540	-44	-3
21 0550	-47	-5
21 0560	-45	-5
21 0570	-45	-5
21 0580	-45	-4
21 0590	-45	-4
21 0610	-45	-7
21 0670	-45	-6
21 0680	-45	-2
21 0690	-46	-3
21 0700	-47	-6
21 0710	-44	-3
21 0730	-44	-7
21 0740	-45	-4
21 0750	-44	-2
21 0760	-45	-3
21 0770	-44	-6
21 0800	-45	-7
21 0810	-46	-4
21 0820	-45	-5
21 0830	-45	-3
21 0840	-45	-3
21 0850	-45	-4
21 0860	-45	-3
21 0870	-45	-4
21 0880	-44	-4
21 0890	-45	-5
21 0900	-47	-6
21 0910	-45	-5
21 0920	-44	-3
21 0930	-44	-3
21 0950	-47	-8
21 0960	-44	-3
21 0970	-46	-8
21 0980	-45	-2
21 0990	-45	-4
21 1020	-44	-3
21 1050	-45	-3
21 1070	-44	-6
21 1080	-45	-7
21 1120	-44	-3
21 1130	-44	-3
21 1160	-46	-7
21 1180	-47	-6
21 1200	-46	-8
21 1210	-44	-4
21 1230	-45	-6
21 1240	-46	-2
21 1270	-44	-4
21 1280	-45	-3
21 1290	-45	-4
21 1300	-45	-4
51 0010	-56	-15
51 0030	-53	-18
51 0040	-54	-17
51 0050	-57	-15
51 0120	-53	-17
51 0130	-57	-14
51 0140	-59	-11
51 0160	-56	-17
51 0250	-56	-14
51 0340	-53	-14
51 0390	-53	-16
51 0420	-54	-16
51 0460	-55	-17
51 0550	-60	-14
51 0600	-57	-14
51 0610	-57	-16
51 0650	-57	-17
51 0670	-53	-17
51 0770	-56	-15
51 0780	-55	-17
51 0810	-53	-16
51 0820	-53	-16
51 0840	-56	-16
52 0490	-46	-13
52 1800	-49	-13
52 2020	-50	-13


More information about the Gretl-users mailing list