I have a dataset of 3D coordinates with a length of about $ 4\times 10^6 $.
From this volume I am sequentially selecting coordinates along one axis and manipulating this subset.
My question: Can the Select
function be replaced by something that is faster.
Here is the example code with the needed time for selection:
SeedRandom[1];
coordinates = RandomReal[10, {4000000, 3}]; // AbsoluteTiming
{0.0989835, Null}
selectedCoordinates = Select[coordinates, #[[1]] > 6 && #[[1]] < 7 & ]; // AbsoluteTiming
{5.88215, Null}
Dimensions[selectedCoordinates]
{400416, 3}
Pick[coordinates, 6 < # < 7 & /@ coordinates[[All, 1]]]
is almost twice as fast asSelect[..]
$\endgroup$Select
:compiled = Compile[{{coords, _Integer, 2}}, Select[coords, #[[1]] > 6 && #[[1]] < 7 &], CompilationTarget -> "C"]
. Thencompiled[coordinates]
takes 0.2 secs on my machine. $\endgroup$Cases[coordinates, {x_, y_, z_} /; x > 6 && y < 7]
Assuming that you want to get #[[1]]>6 &&#[[2]]<7. Otherwise the output would always by {}. No integer can be >6 and <7 at the same time ,-). $\endgroup$RandomReal
. $\endgroup$