I can't seem to wrap my head around the syntax for how to rewrite this chunk of code I have using a while loop, and would appreciate some help. I'm going through a list and am grouping the values into clusters based off distance at different time steps. Now for this particular time step,since I know the number of groupings, so I was able to write the code as such, and store these five groups in bFill
like so:
update = {215.747, 215.238, 213.231, 213.162, 212.202, 212.17, 211.369,211.341, 211.017, 210.873, 210.802, 210.649, 210.586, 210.484, 210.4, 210.32, 210.287, 210.198, 210.18, 210.048, 210.047, 210.01, 209.971, 209.958, 209.889, 209.846, 209.819, 209.815, 209.77, 209.754, 209.538, 209.498, 209.452, 209.296, 209.162, 209.136, 209.039, 209.015, 209.007, 208.935, 208.78, 208.671, 208.555, 208.51, 208.3, 208.169, 208.143, 137.806, 137.792, 137.68, 136.943, 136.324, 136.087, 135.653, 135.405, 135.31, 134.587, 132.697, 131.655, 131.253, 124.246, 123.571, 123.543, 123.357, 123.277, 123.079, 122.833, 122.53, 122.433, 122.377, 122.21, 122.192, 122.174, 122.109, 122.097, 121.866, 121.632, 121.619, 121.519, 121.51, 121.415, 121.392, 121.362, 121.303, 121.075, 121.048, 120.652, 120.602, 120.282, 120.209, 119.208, 119.08, 118.918, 118.843, 118.836, 118.655, 118.628, 118.481, 117.876, 117.574, -165.322, -165.584, -165.791, -166.015, -166.172, -166.33, -168.422, -168.681, -169.081, -169.347, -169.633, -170.288, -170.564, -170.79, -172.107, -172.116, -173.292, -173.563, -173.587, -174.051, -174.143, -174.261, -174.274, -174.346, -174.528, -174.978, -175.021, -175.079, -175.096, -175.14, -175.165, -175.295, -175.69,-175.802, -175.88, -175.909, -176.06, -176.177, -176.386, -176.464, -176.547, -176.55, -177.538, -178.261, -178.486, -210.531, -211.429,-211.476, -211.726, -211.752, -211.851, -211.886, -211.891, -212.046, -212.12, -212.327, -212.75, -212.873, -213.008, -213.154, -213.261, -213.275, -213.438, -213.682, -213.834, -213.921, -213.949, -213.98, -213.986, -214.185, -214.307, -214.389, -214.422, -214.763, -214.91,-215.697, -215.767, -215.799, -216.192, -216.203, -216.217, -216.236, -216.256, -216.412, -216.518, -216.796, -216.841, -216.873, -217.059, -217.2, -217.382, -217.454, -217.576, -217.655, -217.752, -218.143, -218.216, -218.301, -218.343, -224.348};
diam = 14.2535;
bFill = {};
branch = {};
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, update[[len + 1]], {All, diam}];
update = DeleteCases[update, Alternatives @@ branch];
AppendTo[bFill, branch];
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, list[[len + 1]], {All, diam}];
update = DeleteCases[update, Alternatives @@ branch];
AppendTo[bFill, branch];
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, list[[len + 1]], {All, diam}];
update = DeleteCases[update, Alternatives @@ branch];
AppendTo[bFill, branch];
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, list[[len + 1]], {All, diam}];
update = DeleteCases[update, Alternatives @@ branch];
AppendTo[bFill, branch];
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, list[[len + 1]], {All, diam}];
Now for this code I would "know" that I'm done when Length@update == 0
, which is the case in the last step, so I was thinking of trying to re-write it like so:
(* update and diam same as from previous *)
bFill = {};
branch = {};
While[Length@update > 0,
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, update[[len + 1]], {All, diam}];
update = DeleteCases[update, Alternatives @@ branch];
AppendTo[bFill, branch];
]
but this just ends up throwing out all sorts of errors, so clearly I'm not writing it correctly. In the end, I'm expecting bFill
to look like so:
[In]:= bFill
[Out]:= {{215.747, 215.238, 213.231, 213.162, 212.202, 212.17, 211.369,211.341, 211.017, 210.873, 210.802, 210.649, 210.586, 210.484,210.4, 210.32, 210.287, 210.198, 210.18, 210.048, 210.047, 210.01,209.971, 209.958, 209.889, 209.846, 209.819, 209.815, 209.77,209.754, 209.538, 209.498, 209.452, 209.296, 209.162, 209.136,209.039, 209.015, 209.007, 208.935, 208.78, 208.671, 208.555,208.51, 208.3, 208.169, 208.143}, {137.806, 137.792, 137.68, 136.943, 136.324, 136.087, 135.653, 135.405, 135.31, 134.587, 132.697, 131.655, 131.253, 124.246, 123.571}, {123.543, 123.357,123.277, 123.079, 122.833, 122.53, 122.433, 122.377, 122.21, 122.192, 122.174, 122.109, 122.097, 121.866, 121.632, 121.619, 121.519, 121.51, 121.415, 121.392, 121.362, 121.303, 121.075, 121.048, 120.652, 120.602, 120.282, 120.209, 119.208, 119.08, 118.918, 118.843, 118.836, 118.655, 118.628, 118.481, 117.876,117.574}, {-165.322, -165.584, -165.791, -166.015, -166.172,-166.33, -168.422, -168.681, -169.081, -169.347, -169.633, -170.288, -170.564, -170.79, -172.107, -172.116, -173.292, -173.563, -173.587, -174.051, -174.143, -174.261, -174.274, -174.346, -174.528, -174.978, -175.021, -175.079, -175.096, -175.14, -175.165, -175.295, -175.69, -175.802, -175.88, -175.909, -176.06, -176.177, -176.386, -176.464, -176.547, -176.55, -177.538, -178.261, -178.486}}
I can't seem to make sense to myself how to re-write this in a recursive fashion, so any help would be appreciated.
Edit: I was just trying a simple While
loop like so:
l = Table[1, 5];
n = 1;
While[
Length@l > 0, n = n*Length@l;
l = Delete[l, 1]
]
n
(* 120 *)
which works, just as a sanity check that I can indeed use the length of update
as the condition for the While
loop. I thought maybe with some moving things around it might work:
While[
Length@update > 0,
len = Total[Table[Length@bFill[[i]], {i, Length@bFill}]];
branch = Nearest[update, update[[len + 1]], {All, diam}];
AppendTo[bFill, branch];
update = DeleteCases[update, Alternatives @@ branch]
]
but again, same error. I noticed that while bFill[[1]]
contained the values I expected, bFill[[2]]
contained more values (43 vs the 15 it should have had) and missed some that values I would have expected it to pick up. SO something about how this is being written isn't translating correctly when I try to put it into a While
loop.
While
is checked before the second argument is executed. So the loop does not start ifupdate
is undefined or{}
. It should suffice to defineupdate={1}
before the loop. $\endgroup$AppendTo
in a loop in Mathematica is doomed to be slow when the list you accumulate becomes long... Either pre-allocate the list first using e.g.ConstantArray
and then modify elements, or better, useTable
. $\endgroup$update
anddiam
are defined outside of theWhile
loop, so I don't understand why this would be a problem or whyupdate
would be redefined asupdate={1}
- I need the data that is initially stored inupdate
. $\endgroup$Reap
+Sow
. $\endgroup$AppendTo
inside theWhile
bySow
, and wrap the whole thing in aReap
on the outside. $\endgroup$