[Gretl-users] matrix indexes question after sort

Allin Cottrell cottrell at wfu.edu
Thu Apr 24 13:24:47 EDT 2008


On Thu, 24 Apr 2008, Franck Nadaud wrote:

> 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.,,

> 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...

See if this does what you're looking for:

<script>
matrix xc = X_COORD
matrix yc = Y_COORD
scalar n = rows(xc)
scalar m = 4
matrix nnlist = zeros(n,m)
matrix idx = transp(seq(1,n))
scalar xi, yi

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

print nnlist
</script>

Allin.


More information about the Gretl-users mailing list