I have a dataSet
that I'm trying to sort choosing a starting point and going to its nearest neighbor, and then to its nearest neighbor and so on. To do so, I've written the following code (wich im sure is not optimal)
ordDataSet = NestList[With[{elem = Nearest[#[[1]], #[[2]]][[1]]},
{Select[#[[1]], (# != elem &)], elem}] &,
{Delete[dataSet, {startIdx}], dataSet[[startIdx]]}, 300][[All,2]];
What the code does is given the starting point dataSet[[startIdx]]
and the list Delete[dataSet,{startIdx}]
, uses Nearest
to find the nearest neighbor elem
, and return the pair {newDataSet,elem}
where the new list is the original data set minus the starting point and the nearest neighbor. Then it nests.
At first glance this works very well, but for some reason, it starts misbehaving at some point. For the actual dataSet
dataSet ={{0,107.212,-166.757},{0,104.104,-164.502},{0,112.403,-150.885},
{0,115.978,-158.598},{0,105.259,-163.986},{0.00417,107.759,-166.45},
{0.00417,104.471,-164.266},{0.00417,107.026,-151.875},{0.00417,105.777,-163.744},
{0.00417,116.652,-158.168},{0.00833,108.316,-166.138},{0.00833,104.845,-164.025},
{0.00833,113.825,-150.301},{0.00833,117.3,-157.713},{0.00833,106.307,-163.497},
{0.0125,114.546,-150.},{0.0125,117.949,-157.25},{0.0125,105.226,-163.78},
{0.0125,108.883,-165.822},{0.0125,106.855,-163.25},{0.0167,118.582,-156.766},
{0.0167,109.459,-165.5},{0.0167,101.6,-140.503},{0.0167,115.272,-149.69},
{0.0167,107.423,-163.003},{0.0167,105.617,-163.531},{0.0208,110.046,-165.175},
{0.0208,108.007,-162.753},{0.0208,119.185,-156.256},{0.0208,106.019,-163.277},
{0.0250,119.72,-155.695},{0.0250,110.631,-164.839},{0.0250,106.428,-163.019},
{0.0250,116.72,-149.036},{0.0250,108.607,-162.502},{0.0292,120.192,-155.087},
{0.0292,111.232,-164.501},{0.0292,109.223,-162.248},{0.0292,117.451,-148.698},
{0.0292,106.845,-162.755},{0.0333,107.27,-162.487},{0.0333,117.414,-152.444},
{0.0333,118.18,-148.348},{0.0333,120.441,-154.333},{0.0333,111.84,-164.158},
{0.0375,110.494,-161.728},{0.0375,107.703,-162.214},{0.0375,118.904,-147.984},
{0.0375,112.455,-163.807},{0.0417,111.158,-161.467},{0.0417,119.618,-147.603},
{0.0417,113.078,-163.451},{0.0417,108.146,-161.937},{0.0458,108.595,-161.654},
{0.0458,113.709,-163.089},{0.0458,111.838,-161.204},{0.0500,112.534,-160.938},
{0.0500,112.654,-141.388},{0.0500,114.345,-162.72},{0.0500,109.044,-161.363},
{0.0500,112.524,-160.933},{0.0500,120.969,-146.763},{0.0542,113.234,-160.665},
{0.0542,121.605,-146.304},{0.0542,109.504,-161.068},{0.0583,109.975,-160.769},
{0.0583,122.156,-145.782},{0.0583,115.631,-161.958},{0.0583,100.877,-151.789},
{0.0583,115.968,-141.83},{0.0625,122.61,-145.19},{0.0625,116.281,-161.565},
{0.0625,110.451,-160.464},{0.0625,101.965,-151.715},{0.0625,114.685,-160.109},
{0.0667,110.933,-160.153},{0.0667,116.931,-161.162},{0.0667,115.445,-159.836},
{0.0667,122.707,-144.366},{0.0708,111.42,-159.836},{0.0708,117.583,-160.748},
{0.0708,104.149,-151.56},{0.0708,116.223,-159.561},{0.0750,111.913,-159.512},
{0.0750,117.022,-159.288},{0.0750,118.22,-160.316},{0.0750,105.254,-151.482},
{0.0792,118.863,-159.875},{0.0792,112.41,-159.182},{0.0792,117.82,-159.},
{0.0792,106.37,-151.402},{0.0833,107.494,-151.318},{0.0833,100.785,-147.01},
{0.0833,112.91,-158.844},{0.0833,119.491,-159.413},{0.0833,118.672,-158.738},
{0.0875,113.414,-158.499},{0.0875,119.579,-158.508},{0.0917,113.919,-158.146},
{0.0917,103.978,-147.28},{0.0958,114.425,-157.784},{0.100,114.93,-157.413},
{0.100,107.059,-147.566},{0.104,113.234,-150.814},{0.104,115.433,-157.031},
{0.108,115.932,-156.638},{0.113,116.423,-156.232},{0.113,101.498,-145.35},
{0.117,116.906,-155.813},{0.117,102.399,-145.22},{0.121,117.374,-155.376},
{0.121,103.297,-145.086},{0.125,117.826,-154.921},{0.125,104.183,-144.944},
{0.129,118.252,-154.444},{0.133,118.644,-153.937},{0.133,105.938,-144.636},
{0.138,118.996,-153.401},{0.138,106.801,-144.466},{0.142,119.308,-152.835},
{0.142,107.658,-144.286},{0.146,119.57,-152.235},{0.146,108.498,-144.09},
{0.150,109.328,-143.88},{0.150,119.787,-151.607},{0.154,119.966,-150.958},
{0.154,110.138,-143.652},{0.158,110.927,-143.404},{0.158,120.106,-150.289},
{0.163,120.211,-149.605},{0.163,111.689,-143.134},{0.167,120.259,-148.896},
{0.167,112.415,-142.835},{0.171,113.099,-142.506},{0.171,119.977,-147.999},
{0.175,120.277,-147.493},{0.175,113.727,-142.137},{0.179,120.589,-146.993},
{0.179,114.291,-141.726},{0.183,114.772,-141.264},{0.183,120.91,-146.499},
{0.188,115.167,-140.749},{0.188,121.239,-146.01},{0.192,121.576,-145.524},
{0.192,115.485,-140.189},{0.196,121.922,-145.041},{0.196,115.723,-139.584},
{0.200,122.274,-144.561},{0.200,115.898,-138.947},{0.204,122.634,-144.083},
{0.204,116.027,-138.288},{0.208,116.109,-137.61},{0.213,123.371,-143.131},
{0.213,116.16,-136.921},{0.217,116.162,-136.215},{0.217,123.739,-142.65},
{0.221,124.081,-142.15},{0.221,116.199,-135.54},{0.225,124.382,-141.622},
{0.225,115.642,-134.569},{0.229,124.612,-141.048},{0.229,115.929,-134.06},
{0.233,124.705,-140.391},{0.233,116.244,-133.565},{0.238,124.403,-139.499},
{0.238,116.576,-133.078},{0.242,116.922,-132.599},{0.242,120.971,-137.197},
{0.246,120.605,-136.819},{0.246,117.284,-132.126},{0.250,117.66,-131.659},
{0.250,120.585,-136.614},{0.254,120.676,-136.469},{0.254,118.049,-131.197},
{0.258,120.824,-136.357},{0.258,118.452,-130.74},{0.263,121.006,-136.266},
{0.263,118.869,-130.287},{0.267,121.211,-136.19},{0.267,119.287,-129.833},
{0.271,121.431,-136.125},{0.271,119.665,-129.354},{0.275,119.977,-128.839},
{0.275,121.662,-136.069},{0.279,121.901,-136.021},{0.279,120.191,-128.271},
{0.283,122.148,-135.978},{0.283,120.266,-127.633},{0.288,122.4,-135.941},
{0.288,120.148,-126.902},{0.292,122.655,-135.908},{0.292,119.772,-126.057},
{0.296,122.915,-135.88},{0.296,119.114,-125.106},{0.300,123.178,-135.855},
{0.300,118.23,-124.103},{0.304,123.442,-135.833},{0.304,117.299,-123.157},
{0.308,123.71,-135.815},{0.308,116.465,-122.334},{0.313,123.978,-135.799},
{0.313,115.785,-121.644},{0.317,115.25,-121.066},{0.317,124.249,-135.786},
{0.321,124.521,-135.776},{0.321,114.834,-120.578},{0.325,124.794,-135.768},
{0.325,114.516,-120.159},{0.329,125.068,-135.763},{0.329,114.275,-119.795},
{0.333,114.099,-119.476},{0.333,125.343,-135.759},{0.338,125.619,-135.759},
{0.338,113.974,-119.193},{0.342,125.902,-135.762},{0.342,113.892,-118.939},
{0.346,113.848,-118.71},{0.346,126.18,-135.766},{0.350,113.834,-118.503},
{0.350,126.46,-135.772},{0.354,113.847,-118.313},{0.354,126.739,-135.78},
{0.358,113.886,-118.14},{0.358,127.018,-135.79},{0.363,127.298,-135.802},
{0.363,113.943,-117.98},{0.367,127.579,-135.816},{0.367,114.018,-117.833},
{0.371,114.109,-117.695},{0.371,127.86,-135.832},{0.375,128.142,-135.85},
{0.375,114.215,-117.568},{0.379,128.424,-135.871},{0.379,114.333,-117.449},
{0.383,114.462,-117.338},{0.383,128.706,-135.893},{0.388,114.602,-117.233},
{0.388,128.989,-135.918},{0.392,114.75,-117.135},{0.392,129.272,-135.945},
{0.396,129.556,-135.975},{0.396,114.907,-117.043},{0.400,129.84,-136.006},
{0.400,115.07,-116.956},{0.404,115.241,-116.874},{0.404,130.124,-136.04},
{0.408,115.417,-116.796},{0.408,130.409,-136.077},{0.413,115.599,-116.723},
{0.413,130.694,-136.116},{0.417,115.785,-116.653},{0.417,130.979,-136.158},
{0.421,115.976,-116.588},{0.421,131.265,-136.202},{0.425,131.551,-136.249},
{0.425,116.17,-116.525},{0.429,116.368,-116.466},{0.429,131.837,-136.3},
{0.433,116.568,-116.41},{0.433,132.123,-136.353},{0.438,116.772,-116.357},
{0.438,132.41,-136.409},{0.442,116.977,-116.307},{0.442,132.697,-136.468},
{0.446,132.985,-136.531},{0.446,117.186,-116.259},{0.450,133.273,-136.597},
{0.450,117.395,-116.214},{0.454,133.56,-136.668},{0.454,117.606,-116.171},
{0.458,133.849,-136.742},{0.458,117.819,-116.131},{0.463,118.033,-116.093},
{0.463,134.137,-136.82},{0.467,118.247,-116.057},{0.467,134.426,-136.903},
{0.471,134.715,-136.991},{0.471,118.462,-116.024},{0.475,135.004,-137.085},
{0.475,118.678,-115.992},{0.479,135.294,-137.184},{0.479,118.895,-115.963},
{0.483,135.584,-137.29},{0.483,119.112,-115.936},{0.488,135.874,-137.402},
{0.488,119.328,-115.911},{0.492,136.164,-137.523},{0.492,119.544,-115.888},
{0.496,136.455,-137.654},{0.496,119.761,-115.868},{0.500,119.977,-115.849},
{0.500,136.746,-137.795},{0.504,120.193,-115.833},{0.504,137.038,-137.95},
{0.508,120.411,-115.819},{0.508,137.331,-138.12},{0.513,120.627,-115.807},
{0.513,137.625,-138.31},{0.517,137.922,-138.525},{0.517,120.839,-115.797},
{0.521,121.052,-115.789},{0.521,138.222,-138.775},{0.525,121.263,-115.784},
{0.525,143.043,-147.527},{0.525,138.528,-139.074},{0.529,121.473,-115.78},
{0.529,143.1,-147.916},{0.529,138.847,-139.456},{0.533,121.682,-115.779},
{0.533,143.082,-148.164},{0.533,139.204,-140.01},{0.533,140.401,-143.007},
{0.538,121.89,-115.781},{0.538,143.053,-148.367},{0.542,122.095,-115.784},
{0.542,143.011,-148.517},{0.546,122.3,-115.79},{0.546,142.974,-148.641},
{0.550,142.948,-148.745},{0.550,122.502,-115.799},{0.554,142.94,-148.841},
{0.554,122.701,-115.81},{0.558,142.942,-148.914},{0.558,122.898,-115.823},
{0.563,142.963,-148.976},{0.563,123.092,-115.84},{0.567,123.282,-115.858},
{0.567,143.003,-149.027},{0.571,143.063,-149.07},{0.571,123.468,-115.88},
{0.575,143.145,-149.11},{0.575,123.651,-115.904},{0.579,123.829,-115.931},
{0.579,143.247,-149.136},{0.583,124.002,-115.961},{0.583,143.37,-149.154},
{0.588,124.169,-115.995},{0.588,143.515,-149.166},{0.592,124.329,-116.031},
{0.592,143.684,-149.17},{0.596,143.873,-149.168},{0.596,124.481,-116.07},
{0.600,144.087,-149.16},{0.600,124.625,-116.112},{0.604,124.759,-116.158},
{0.604,144.324,-149.146},{0.608,124.882,-116.206},{0.608,144.583,-149.125},
{0.613,124.991,-116.258},{0.613,144.858,-149.102},{0.617,125.086,-116.313},
{0.617,145.161,-149.069},{0.621,125.164,-116.371},{0.621,145.489,-149.03},
{0.625,125.223,-116.432},{0.625,145.841,-148.985},{0.629,146.216,-148.934},
{0.629,125.261,-116.497},{0.633,146.612,-148.877},{0.633,125.276,-116.563},
{0.638,125.264,-116.632},{0.638,147.033,-148.814},{0.642,125.224,-116.704},
{0.642,147.477,-148.745},{0.646,125.154,-116.776},{0.646,147.943,-148.67},
{0.650,148.431,-148.588},{0.650,125.052,-116.849},{0.654,148.943,-148.5},
{0.654,124.918,-116.923},{0.658,149.477,-148.407},{0.658,124.752,-116.997},
{0.663,150.034,-148.306},{0.663,124.555,-117.069},{0.667,150.611,-148.198},
{0.667,124.329,-117.14},{0.671,124.075,-117.209},{0.671,151.211,-148.084},
{0.675,123.798,-117.275},{0.675,151.833,-147.964},{0.679,152.472,-147.835},
{0.679,123.499,-117.338},{0.683,123.183,-117.398},{0.683,153.136,-147.7},
{0.688,153.821,-147.558},{0.688,122.852,-117.456},{0.692,122.509,-117.51},
{0.692,154.527,-147.409},{0.696,122.155,-117.562},{0.696,155.25,-147.251},
{0.700,121.794,-117.61},{0.700,155.98,-147.08},{0.704,121.425,-117.656},
{0.704,156.746,-146.908},{0.708,121.052,-117.699},{0.708,157.531,-146.729},
{0.713,120.674,-117.74},{0.713,158.338,-146.542},{0.717,120.293,-117.778},
{0.717,159.164,-146.347},{0.721,119.91,-117.814},{0.721,160.011,-146.144},
{0.725,160.876,-145.933},{0.725,119.524,-117.849},{0.729,119.137,-117.881},
{0.729,161.759,-145.713},{0.733,138.983,-129.306},{0.733,132.82,-128.392},
{0.733,118.75,-117.911},{0.733,162.66,-145.485},{0.738,163.577,-145.247},
{0.738,118.362,-117.94},{0.738,141.38,-129.709},{0.738,131.315,-128.279},
{0.742,117.972,-117.967},{0.742,164.514,-145.},{0.742,130.272,-128.245},
{0.742,130.258,-128.242},{0.742,143.342,-130.024},{0.746,129.413,-128.244},
{0.746,117.583,-117.992},{0.746,145.14,-130.295},{0.750,117.193,-118.016},
{0.750,128.681,-128.269},{0.750,166.436,-144.478},{0.750,146.841,-130.531},
{0.754,148.513,-130.75},{0.754,128.023,-128.307},{0.754,167.422,-144.203},
{0.754,116.803,-118.039},{0.758,116.413,-118.06},{0.758,168.401,-143.906},
{0.758,127.418,-128.356},{0.758,150.161,-130.951},{0.758,100.035,-93.1424},
{0.763,126.853,-128.413},{0.763,169.419,-143.611},{0.763,116.023,-118.081},
{0.763,151.804,-131.137},{0.763,101.189,-93.1484},{0.767,115.633,-118.1},
{0.767,153.443,-131.308},{0.767,170.453,-143.306},{0.767,126.319,-128.477},
{0.771,125.809,-128.545},{0.771,171.502,-142.99},{0.771,115.243,-118.118},
{0.775,125.32,-128.618},{0.775,172.565,-142.664},{0.775,114.854,-118.136},
{0.775,104.853,-93.1614},{0.779,124.847,-128.694},{0.779,158.429,-131.756},
{0.779,173.641,-142.326},{0.779,114.465,-118.152},{0.779,106.142,-93.1641},
{0.783,124.389,-128.773},{0.783,160.12,-131.884},{0.783,114.076,-118.168},
{0.783,174.729,-141.976},{0.783,107.439,-93.1533},{0.788,161.832,-132.005},
{0.788,175.829,-141.614},{0.788,113.687,-118.184},{0.788,123.943,-128.854},
{0.792,123.508,-128.938},{0.792,113.299,-118.199},{0.792,163.566,-132.118},
{0.792,176.939,-141.239},{0.792,110.183,-93.1532},{0.796,123.082,-129.023},
{0.796,111.604,-93.1513},{0.796,178.059,-140.849},{0.796,112.911,-118.213},
{0.796,165.333,-132.228},{0.800,122.664,-129.109},{0.800,179.152,-140.426},
{0.800,112.522,-118.228},{0.800,167.121,-132.328},{0.800,113.059,-93.1484},
{0.804,122.253,-129.197},{0.804,180.284,-140.006},{0.804,112.134,-118.242},
{0.804,168.949,-132.43},{0.804,114.547,-93.1443},{0.808,116.067,-93.1385},
{0.808,121.849,-129.286},{0.808,111.747,-118.257},{0.808,181.418,-139.568},
{0.808,170.806,-132.527},{0.813,182.55,-139.11},{0.813,121.45,-129.376},
{0.813,172.714,-132.631},{0.813,111.36,-118.272},{0.817,174.666,-132.739},
{0.817,110.973,-118.287},{0.817,183.673,-138.627},{0.817,121.056,-129.466},
{0.817,119.203,-93.1213},{0.821,120.667,-129.558},{0.821,184.733,-138.086},
{0.821,110.587,-118.302},{0.821,176.687,-132.865},{0.821,120.818,-93.1095},
{0.825,110.2,-118.318},{0.825,120.282,-129.65},{0.825,185.8,-137.531},
{0.825,122.463,-93.095},{0.829,109.814,-118.335},{0.829,186.812,-136.925},
{0.829,119.9,-129.743},{0.829,124.138,-93.0775},{0.833,187.63,-136.179},
{0.833,109.428,-118.353},{0.833,119.522,-129.836},{0.833,125.841,-93.0562},
{0.833,101.434,-76.7516},{0.833,183.346,-133.463},{0.838,109.043,-118.371},
{0.838,119.147,-129.929},{0.838,127.53,-93.0084},{0.838,188.095,-135.196},
{0.838,186.301,-134.068},{0.838,103.785,-77.2176},{0.842,118.774,-130.023},
{0.842,108.657,-118.391},{0.842,129.287,-92.978},{0.846,108.272,-118.412},
{0.846,108.625,-78.1698},{0.846,118.404,-130.118},{0.850,118.036,-130.212},
{0.850,107.886,-118.434},{0.850,132.878,-92.9027},{0.854,117.67,-130.308},
{0.854,134.71,-92.8557},{0.854,113.665,-79.1537},{0.854,107.502,-118.459},
{0.858,107.117,-118.485},{0.858,117.305,-130.403},{0.858,116.271,-79.6627},
{0.863,116.942,-130.499},{0.863,118.94,-80.1854},{0.863,106.733,-118.513},
{0.863,138.437,-92.7383},{0.867,116.581,-130.595},{0.867,106.35,-118.543},
{0.867,121.666,-80.7181},{0.871,116.22,-130.692},{0.871,105.966,-118.576},
{0.871,142.17,-92.5433},{0.875,105.583,-118.611},{0.875,144.07,-92.4357},
{0.875,115.861,-130.789},{0.875,127.349,-81.8495},{0.879,105.2,-118.649},
{0.879,115.502,-130.886},{0.879,145.973,-92.3099},{0.883,115.144,-130.984},
{0.883,104.818,-118.69},{0.883,133.39,-83.0948},{0.888,149.743,-91.9766},
{0.888,114.786,-131.082},{0.888,104.437,-118.735},{0.888,136.58,-83.7813},
{0.892,151.488,-91.6963},{0.892,104.056,-118.783},{0.892,114.429,-131.182},
{0.896,114.072,-131.281},{0.896,153.213,-91.3845},{0.896,103.676,-118.834},
{0.900,113.714,-131.382},{0.900,103.296,-118.889},{0.900,154.775,-90.9557},
{0.904,155.686,-90.1154},{0.904,102.917,-118.948},{0.904,113.357,-131.483},
{0.908,102.54,-119.011},{0.908,113.,-131.585},{0.913,112.642,-131.687},
{0.913,102.163,-119.078},{0.917,101.787,-119.149},{0.917,112.284,-131.791},
{0.921,111.926,-131.895},{0.921,101.412,-119.225},{0.925,111.566,-132.001},
{0.925,101.038,-119.305},{0.929,111.206,-132.108},{0.929,100.666,-119.39},
{0.933,110.845,-132.216},{0.933,100.294,-119.479},{0.938,110.483,-132.326},
{0.942,110.12,-132.437},{0.946,109.756,-132.55},{0.950,109.392,-132.665},
{0.954,109.025,-132.781},{0.958,108.657,-132.9},{0.963,108.288,-133.02},
{0.967,107.917,-133.143},{0.971,107.545,-133.268},{0.975,107.171,-133.396},
{0.979,106.797,-133.527},{0.983,106.42,-133.66},{0.988,106.043,-133.796},
{0.992,105.664,-133.936},{0.996,105.284,-134.078},{1.00,104.903,-134.225}};
Here is a 3d representation of the dataSet
Starting the code at the last point of dataSet
ordDataSet = NestList[With[{elem = Nearest[#[[1]], #[[2]]][[1]]},
{Select[#[[1]], (# != elem &)], elem}] &,
{Delete[dataSet, {-1}], dataSet[[-1]]}, 300][[All, 2]];
works very well, until step 243 where everithing goes bad (sorry for the huge gif)
Looking at the 3d plot, is clear that the nearest neighbor is ordDataSet[[245]]
, but manually calculating the distance confirms that the nearest point is ordDataSet[[244]]
.
What is wrong then, my code, the displayed plot or the calculation of the Norm?
EDIT
I hope I have not missed the point but the problem is you are probably using
BoxRatios->1
which can affect the judgement.
I tried to see this before posting, but using AspectRatio
instead of BoxRatio
. Lucky for me, the small dimension plays a crucial role in connectendness, and it should be rescaled to wiegth as much as the yz coordinates.
Rescaling the data:
rescDataSet = MapAt[100# &, dataSet, {All,1}];
rescOrdDataSet = NestList[With[{elem = Nearest[#[[1]], #[[2]]][[1]]},
{Select[#[[1]], (# != elem &)], elem}] &,
{Delete[scaledDataSet, {-1}], scaledDataSet[[-1]]}, 300][[All, 2]];
everithing goes as expected:
Thank you very much Kuba!