5
$\begingroup$

Here is my list. The first column contains its index and second column has data.

list={{1, 170.93}, {2, 170.4}, {3, 174.08}, {4, 160.65}, {5, 166.44}, {6, 
      169.75}, {7, 175.86}, {8, 177.6}, {9, 180.27}, {10, 182.82}, {11, 
      182.02}, {12, 186.65}, {13, 184.53}, {14, 186.17}, {15, 
     177.29}, {16, 172.06}, {17, 175.58}, {18, 168.09}, {19, 
     172.33}, {20, 165.1}, {21, 154.88}, {22, 161.28}, {23, 165.25}, {24,
     165.15}, {25, 171.03}, {26, 174.55}, {27, 182.57}, {28, 
     191.02}, {29, 191.11}, {30, 184.93}, {31, 188.85}, {32, 191.44}};

I want to delete all numbers that are smaller than the last largest element. For example, I want to delete 4th, 5th and 6th elements because they are smaller than the last largest element (3rd). Similarly, I want to delete elements from 13th to 27th elements because they are smaller than the last largest element (12th). 30th and 31st elements should be deleted because they are smaller than prior largest element (29th). Final results should contain increasing numbers. Here is the tabular representation of the data:

enter image description here

I did my best to explain my problem. Please write in the comment if it is not clear. I have a big data and I need to manipulate it as explained here. Any help would be greatly appreciated. Thanks

$\endgroup$
5
  • 4
    $\begingroup$ DeleteDuplicatesBy[ Transpose[{list[[All, 1]], FoldList[Max, list[[All, 2]]]}], Last] should do what you're after. $\endgroup$
    – ciao
    Commented Jul 6, 2019 at 18:05
  • $\begingroup$ Should you also be deleting the second element since it is smaller than the first? I will note that the solution from @ciao does remove the second element. $\endgroup$
    – Mark R
    Commented Jul 6, 2019 at 18:11
  • $\begingroup$ @ Mark R, yes you are right. I missed it. Thanks $\endgroup$
    – DLT
    Commented Jul 6, 2019 at 18:37
  • 2
    $\begingroup$ DeleteDuplicates[list, #1[[2]] > #2[[2]] &] $\endgroup$
    – Bob Hanlon
    Commented Jul 6, 2019 at 18:37
  • $\begingroup$ @ Bob, thanks for your simple answer. I really appreciate it. $\endgroup$
    – DLT
    Commented Jul 6, 2019 at 23:51

3 Answers 3

5
$\begingroup$
TakeList[list, Length /@ Split[FoldList[Max, list[[All, 2]]]]][[All, 1]]

{{1, 170.93}, {3, 174.08}, {7, 175.86}, {8, 177.6}, {9, 180.27}, {10, 182.82}, {12, 186.65}, {28, 191.02}, {29, 191.11}, {32, 191.44}}

Also

max = list[[1, -1]];
Split[list, Or[Last[#2] < max, max = Last[#2]] &][[All, 1]]

same result

DeleteDuplicates[Join @@ FoldList[MaximalBy[Last] @* Append, {First @ list}, 
   Rest @ list]]

same result

The first method is much faster.

$\endgroup$
2
  • $\begingroup$ Thank you kglr. $\endgroup$
    – DLT
    Commented Jul 6, 2019 at 19:24
  • $\begingroup$ @DestinationLess_Traveller, thank you for the accept. $\endgroup$
    – kglr
    Commented Jul 6, 2019 at 19:25
2
$\begingroup$

Using IncreasingFilter by Sander Huisman

IncreasingFilter = ResourceFunction["IncreasingFilter"];

Query[Position[list, #][[1, 1]] & /@ 
   IncreasingFilter[Last /@ list]] @ list

{{1, 170.93}, {3, 174.08}, {7, 175.86}, {8, 177.6}, {9, 180.27}, {10, 182.82}, {12, 186.65}, {28, 191.02}, {29, 191.11}, {32, 191.44}}

$\endgroup$
2
$\begingroup$

A variant using FoldList:

Union@Rest@FoldList[If[#2[[2]] > #1[[2]], #2, #1] &, {0, 0}, list]

{{1, 170.93}, {3, 174.08}, {7, 175.86}, {8, 177.6}, {9, 180.27},
{10, 182.82}, {12, 186.65}, {28, 191.02}, {29, 191.11}, {32, 191.44}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.