0
$\begingroup$

I'm producing a set of coordinates for every iteration in a simulation and saving it into a txt file in the following way:

Table[write[myfile,CoorSet[data,i]],{i,1,1000}]

In CoorSet there is a lenghty calculation that produces the coordinates, it works well, however in same cases there is a double output and some cases there is non.

So the output looks as follows:

{-1,1}
{-1,2}
{0,3}
{{1,-1},{20,12}}
{2,4}
0.01*Round[{}+{-1,-1}] (* the line of calculation*)

How do I replace the written lines output with {0,0} whenever there is the written line.

and whenever there is a double set of coordinates, how can the one that is further away by distance from its neighbors be suppressed such that the table that I will get will be:

{-1,1}
{-1,2}
{0,3}
{1,-1}
{2,4}
{0,0}
$\endgroup$
1
$\begingroup$

Let,s say this is your input

list = {{-1, 1}, {-1, 2}, {0, 3}, {{1, -1}, {20, 12}}, {2, 4}, "0.01*Round[{}+{-1,-1}]"}

First replace the String

list = list /. x__ /; StringQ[x] -> {0, 0}

Join[{list[[1]]},
 Table[s = list[[i]];
  If[Length[Flatten@s] > 2,
  d = Min[{Norm[s[[#]] - list[[i - 1]]], 
    Norm[s[[#]] - list[[i + 1]]]}] & /@ Range[Length[s]];
  d1 = Join[s // Transpose, {d}] // Transpose;
  Sort[d1, #1[[3]] < #2[[3]] &][[1, 1 ;; 2]],
  s],
{i, 2, Length[list] - 1}],
{list[[-1]]}]

{{-1, 1}, {-1, 2}, {0, 3}, {1, -1}, {2, 4}, {0, 0}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.