Message Boards Message Boards

4 Replies
1 Total Likes
View groups...
Share this post:

Nearest neighbor problem

Posted 9 years ago

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

POSTED BY: eft rsd
4 Replies

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 BY: Daniel Lichtblau
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 BY: eft rsd

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 BY: Daniel Lichtblau
Posted 9 years ago

Using a semicolon did not change anything.. Is it possible that Do does not work with Nearest???

POSTED BY: eft rsd
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract