I've a problem when I try to extract some unwanted sublists from a list. Given a test list:
m2test = {{1, 0, 1, 1, 1, 1}, {2, 0, 1, 1, 2, 2}, {3, 0, 1, 1, 3.1,
3}, {4, 0, 1, 1, 3.2, 4}, {5, 0, 1, 1, 3.3, 5}, {6, 0, 1, 1, 1.8,
6}, {7, 0, 1, 1, 1.5, 7}, {8, 0, 1, 1, 1.55, 8}, {9, 0, 1, 1, 1.6,
9}, {10, 0, 1, 1, 1.4, 10}};
Using the following for loops:
m3 = {{"Index", "Track ID", "x (um)", "y (um)", "z (um)", "t (s)"}};
n = Max[m2test[[All, 2]]] // IntegerPart;
For[i = 0, i <= n, i++,
p = Pick[m2test, m2test[[All, 2]], i];
For[j = 2, j <= Length[p] - 1, j++,
If[Abs[p[[(j), 5]] - p[[j - 1, 5]]] > 1 ||
Abs[p[[(j), 5]] - p[[j + 1, 5]]] > 1,
p = Cases[p, Except[p[[j]]]], p]];
m3 = Join[m3, p]];
I wanted to remove all those sublists whose 5th elements were greater or smaller than those in the previous and/or following sublists by a value greater than 1 (the first for loop is needed to do this in each subgroup where the 2nd elements are the same, but in m2test there's only a value, so it doesn't matter here).I wanted that, every time a sublist was removed, the list was updated before the next round. Therefore, I was expecting to get:
m3
{{"Index", "Track ID", "x (um)", "y (um)", "z (um)", "t (s)"},{1, 0, 1, 1, 1, 1},{6, 0, 1, 1, 1.8,
6}, {7, 0, 1, 1, 1.5, 7}, {8, 0, 1, 1, 1.55, 8}, {9, 0, 1, 1, 1.6,
9}, {10, 0, 1, 1, 1.4, 10}}
But, instead, I get:
m3
{{"Index", "Track ID", "x (um)", "y (um)", "z (um)", "t (s)"}, {1, 0,
1, 1, 1, 1}, {3, 0, 1, 1, 3.1, 3}, {4, 0, 1, 1, 3.2, 4}, {6, 0, 1,
1, 1.8, 6}, {7, 0, 1, 1, 1.5, 7}, {8, 0, 1, 1, 1.55, 8}, {9, 0, 1,
1, 1.6, 9}, {10, 0, 1, 1, 1.4, 10}}
What's wrong? Obviously this code, if working as expected, has the drawback of removing also some desired points, like {2, 0, 1, 1, 2, 2}
, or the fact that, if the first point is undesirable, the entire sublist is removed.
Many thanks.
m3
.Could you elaborate it further? $\endgroup$