Il existe probablement une solution simple à cela, mais je ne l'ai pas encore trouvée. J'ai un tableau des identifiants d'emplacement avec les emplacements X et Y. J'ai besoin d'identifier l'emplacement le plus proche et de calculer la distance euclidienne jusqu'à cet emplacement.La distance ne semble pas trop lourde mais j'ai du mal à trouver l'emplacement le plus proche. Le fichier est joint. Merci.
Tableau KD () est probablement ce dont vous avez besoin.
@Craige_Hales a un article de blog: Voisins
Salut Peter, cela utilise la plate-forme KNN pour obtenir les lignes et les distances voisines, puis écrit l'ID du voisin dans une nouvelle colonne.
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;
Reprenant «KDTable ()» mentionné par @Jeff_Perkinson , voici un exemple:
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 ) ) )
);