I have a huge nested list in the following general format:
$\textrm{originaltab} = \{e_i^j\} = \{\{e_1^1,e_1^2,e_1^3,e_1^4,e_1^5,e_1^6,e_1^7,e_1^8\},\{e_2^1,e_2^2,e_2^3,e_2^4,e_2^5,e_2^6,e_2^7,e_2^8\},\cdots\}$
with 8 elements per sublist and a total of around 1,000,000 sublists in the nested list $\textrm{originaltab}$.
What I need to do is the following:
1) Sort the sublists by increasing values of the elements $e_i^3$. This is easy to do using the command Sort in Mathematica:
Sort[originaltab, #1[[3]] < #2[[3]] &]
2) Now I need to define a new nested list, let me call it $\textrm{newtab}$, which consists of all the sublists in $\textrm{originaltab}$ satisfying the following criteria:
$(e_i^1 \neq e_j^1 \, || \, e_i^2 \neq e_j^2) \,\&\& \, (e_j^3\in [e_i^3-\epsilon,e_i^3+\epsilon] \,\&\& \, e_j^4\in [e_i^4-\delta,e_i^4+\delta]),\, i\neq j$
where $\epsilon$ and $\delta$ are some small positive numbers. Since I sorted the sublists in $\textrm{originaltab}$ such that they are ordered by increasing values of $e_i^3$, the sublists in $\textrm{originaltab}$ which eventually satisfy the above criteria are necessarily close to each other, such that it would be a waste of time to compare the first sublist in $\textrm{originaltab}$ with the last one, because they will obviously not satisfy the above criteria, since $e_{1000000}^3-e_1^3\gg\epsilon$. The sublists in $\textrm{newtab}$ should be sorted as in the case of $\textrm{originaltab}$.
3) Once I have defined $\textrm{newtab}$, as schematically depicted above, I need to define a new nested list, which I will call $\textrm{auxiliarytab}$, which should be composed as follows: for all the sublists in $\textrm{newtab}$ satisfying $(e_j^3\in [e_i^3-\epsilon,e_i^3+\epsilon] \,\&\& \, e_j^4\in [e_i^4-\delta,e_i^4+\delta])$, we should delete the ones with the highest value of $e_j^5$. This deletion should be immediate, preventing an excluded sublist to be called again to be compared with other sublists (in the case of overlapping zones).
4) Once the step 3 above is done, I need to remove from originaltab the set of sublists contained in $\textrm{auxiliarytab}$. This is easy to do using the command Complement in Mathematica:
finaltab = Complement[originaltab, auxiliarytab]
So, as a very simple illustration of what the algorithm should do, consider the example below, with $\epsilon=0.1$:
originaltab = {{0.3,0.1,50,0,200,0,0,0},{0.4,0.2,50.1,0,600,0,0,0},{0.3,0.2,50.2,0,10,0,0,0},{1.5,0.8,50.3,0,230,0,0,0},{0.1,0.9,123,0,3000,0,0,0}}
newtab = {{0.3,0.1,50,0,200,0,0,0},{0.4,0.2,50.1,0,600,0,0,0},{0.3,0.2,50.2,0,10,0,0,0},{1.5,0.8,50.3,0,230,0,0,0}}
auxiliarytab = {{0.3,0.1,50,0,200,0,0,0},{0.3,0.2,50.2,0,10,0,0,0}}
finaltab = {{0.4,0.2,50.1,0,600,0,0,0},{1.5,0.8,50.3,0,230,0,0,0},{0.1,0.9,123,0,3000,0,0,0}}
How could I do steps 2 and 3 above in a efficient way (remember that the actual situation comprises a nested list with around 1,000,000 sublists!)?
I thank in advance for any possible suggestion!
========================
EDIT: below I give a possible way of doing what I want, but this way is extremely inefficient in terms of computation time and memory usage. It is simply infeasible if originaltab has 1,000,000 sublists (but works well for a nested list with just 2,000 sublists, for instance).
Clear[\[Epsilon],\[Delta],originaltab,auxiliarytab,finaltab]
\[Epsilon]=0.1;
\[Delta]=0.1;
originaltab=Sort[{{0.3,0.1,50,0,200,0,0,0},{0.4,0.2,50.1,0,600,0,0,0},{0.3,0.2,50.2,0,10,0,0,0},{1.5,0.8,50.3,0,230,0,0,0},{0.1,0.9,123,0,3000,0,0,0}},#1[[3]]<#2[[3]]&]
auxiliarytab=Sort[DeleteDuplicates[DeleteMissing[Flatten[Table[
If[(originaltab[[i, 1]] != originaltab[[j, 1]] ||
originaltab[[i, 2]] !=
originaltab[[j, 2]]) && (IntervalMemberQ[
Interval[{originaltab[[i, 3]] - \[Epsilon],
originaltab[[i, 3]] + \[Epsilon]}],
originaltab[[j, 3]]] &&
IntervalMemberQ[
Interval[{originaltab[[i, 4]] - \[Delta],
originaltab[[i, 4]] + \[Delta]}], originaltab[[j, 4]]]),
DeleteCases[{originaltab[[i, All]], originaltab[[j, All]]},
{_, _, _, _,
Max@{originaltab[[i, 5]], originaltab[[j, 5]]}, _, _, _}
], Missing[]],
{i, 1, Length[originaltab] - 1}, {j, i + 1,
Length[originaltab]}], 2]]], #1[[3]] < #2[[3]] &]
finaltab=Sort[Complement[originaltab,auxiliarytab],#1[[3]]<#2[[3]]&]
axuiliarytab
not in sorting it asSort@RandomReal[1, 1000000] // AbsoluteTiming // First
is only about .15 for me. Unless you're doing real-time manipulation and visualization of the data that's not bad at all. $\endgroup$Compile
should let us handle this. $\endgroup$