# Nearest neighbor problem

Posted 9 years ago
2573 Views
|
4 Replies
|
1 Total Likes
|
 Hi, I am trying to plot lines between nearest neighbors among a group of 6 points using Nearest. I wrote the following code to find the 3 n.n. of every point and tabulate them in a table tt p[1] = {0, 1}; p[2] = {1, 2}; p[3] = {2, 3}; p[4] = {3, 4}; p[5] = {4, 5}; p[6] = {-1, 2}; tab = Table[p[i], {i, 1, 6}]; Do[tt[j] = Nearest[tab, p[j], 3], {j, 1, 6}] sor = Table[{tt[j]}, {j, 1, 6}] But when I run the Do command I get this error message Set::write: Tag Null in Null[1] is Protected. >> I also found that when I apply Nearest to one point only I get the same point as a nearest neighbor like this Nearest[tab, {-1, 2}, 3] Answer-> {{-1, 2}, {0, 1}, {1, 2}} Why I get the same point again? and why I get that error message? Please help me
4 Replies
Sort By:
Posted 9 years ago
 The entered point is in the list and is, obviously, the nearest one to itself. If you want three neighbors not the same as the entered point, request four and drop the first one.
Posted 9 years ago
 Well, I've managed to solve the problem of combining Do with Nearest, but I still get the same point I am looking for its nearest neighbors appearing as its own neighbor.. How to stop that?
Posted 9 years ago
 Try placing a semicolon between the Do and the Table. Without a termination within a compound statement, white space is interpreted as multiplication of the two statements.
Posted 9 years ago
 Using a semicolon did not change anything.. Is it possible that Do does not work with Nearest???