Message Boards Message Boards

0
|
4547 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Sort Array with condition?

Posted 8 years ago

good morning, how can I optimize this code, it is very slow? I am forced to run it several times to get the desired result. example:

i{xnumber, xlevel, xtext) resulting in the list there must be only the xnumber with lower xlevel

i = {{{1.5, 3.2, "a"}, {3.254, 10.5, "b"}, {1.5, 3.3, "c"}, {2.54, 5.6, "d"}, {1.254, 10.5, "e"}, {2.54, 5.4, "f"}, {9.54, 8.6, "g"}}};
maxlevel = 8.4;
FilterMaxLevelAndNumber[i_] :=
  Table[
   yy = Split[Sort[i[[y]]], First[#1] == First[#2] &];
   Select[
    Table[
     Sort[yy[[x]], #1[[2]] < #2[[2]] &][[1]],
     {x, 1, Length[yy]}]
    , #[[2]] <= maxlevel &],
   {y, 1, Length[i]}];

FilterMaxLevelAndNumber[i]
Result: {{{1.5, 3.2, "a"}, {2.54, 5.4, "f"}}}
POSTED BY: Paolo Pellegrini
2 Replies

ok, perfect

POSTED BY: Paolo Pellegrini
Posted 8 years ago
i = {{{1.5, 3.2, "a"}, {3.254, 10.5, "b"}, {1.5, 3.3, "c"}, {2.54, 
     5.6, "d"}, {1.254, 10.5, "e"}, {2.54, 5.4, "f"}, {9.54, 8.6, 
     "g"}}};
maxlevel = 8.4;

Select[Map[Take[Sort[#, #1[[2]] < #2[[2]] &], 1][[1]] &, 
  GatherBy[i[[1]], #[[1]] &]], #[[2]] < maxlevel &]
POSTED BY: Michael Helmle
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract