3
$\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$
  • 4
    $\begingroup$ DeleteDuplicatesBy[ Transpose[{list[[All, 1]], FoldList[Max, list[[All, 2]]]}], Last] should do what you're after. $\endgroup$ – ciao Jul 6 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 Jul 6 at 18:11
  • $\begingroup$ @ Mark R, yes you are right. I missed it. Thanks $\endgroup$ – DLT Jul 6 at 18:37
  • 2
    $\begingroup$ DeleteDuplicates[list, #1[[2]] > #2[[2]] &] $\endgroup$ – Bob Hanlon Jul 6 at 18:37
  • $\begingroup$ @ Bob, thanks for your simple answer. I really appreciate it. $\endgroup$ – DLT Jul 6 at 23:51
4
$\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$
  • $\begingroup$ Thank you kglr. $\endgroup$ – DLT Jul 6 at 19:24
  • $\begingroup$ @DestinationLess_Traveller, thank you for the accept. $\endgroup$ – kglr Jul 6 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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