Probabilmente c'è una soluzione semplice a questo, ma non l'ho ancora capito. Ho una tabella di ID posizione con posizioni X e Y. Devo identificare la posizione più vicina e calcolare la distanza euclidea da quella posizione.La distanza non sembra troppo onerosa ma sto lottando per trovare la posizione più vicina. Il file è allegato. Grazie.
Tabella KD () è probabilmente quello che ti serve.
@Craige_Hales ha un post sul blog: Vicinato
Ciao Peter, questo utilizza la piattaforma KNN per ottenere le righe e le distanze dei vicini, quindi scrive l'ID del vicino in una nuova colonna.
Names Default To Here( 1 );
dt = Current Data Table();
knn = K Nearest Neighbors(
X( :X Location, :Y Location ),
K( 1 ),
save near neighbor rows,
save near neighbor distances
);
knn << close window;
dt << New Column( "Neighbor ID", formula( dt:siteId[:rowNear 1] ) );
dt:Neighbor ID << delete formula;
Riprendendo su "KDTable ()" menzionato da @Jeff_Perkinson , ecco un esempio:
Names Default To Here( 1 );
n = 100; // Size of square
// Make a table of x and y locations
dtx = AsTable((1::n)`, << Invisible);
dty = AsTable((1::n)`, << Invisible);
dt = dty << Join( With( dtx ), Cartesian Join );
Close(dtx, NoSave);
Close(dty, NoSave);
Column(dt, 1) << setName("x");
Column(dt, 2) << setName("y");
dt << deleteProperty("Source");
dt << setName("xy Locations");
// Put these locations into a matrix
mat = dt << getAsMatrix;
tbl = KDTable( mat );
// Pick a point in the interior, so we don't need to decide on any boundary conditions
myX = RandomInteger(2, n-1);
myY = RandomInteger(2, n-1);
myRow = Loc(mat[0, 1] == myX & mat[0, 2] == myY);
myRow = myRow[1];
// Find the 8 nearest neighbours to the chosen point
{rows, dist} = tbl << K nearest rows( 8, myRow );
Show( myRow, rows );
// Check it worked
myXvals = Column(dt, "x")[myRow];
myYvals = Column(dt, "y")[myRow];
dt << NewColumn("Distance", Numeric, Continuous, Formula(sqrt((:x - myXvals)^2 +(:y - myYvals)^2)));
dt << selectRows(VConcat(myRow, rows`));
gb = dt << Graph Builder(
Size( 529, 453 ),
Show Control Panel( 0 ),
Variables( X( :x ), Y( :y ), Color( :Distance ) ),
Elements( Points( X, Y, Legend( 5 ) ) )
);