I want to drop 2 continuous elements every 3 elements,such as I have a list:
list = {3, 5, 5, 5, 2, 5, 1, 0, 5, 0, 4};
I want to get
{3, 5, 5, 5, 1, 0, 4}
Any consice method can do this?
I want to drop 2 continuous elements every 3 elements,such as I have a list:
list = {3, 5, 5, 5, 2, 5, 1, 0, 5, 0, 4};
I want to get
{3, 5, 5, 5, 1, 0, 4}
Any consice method can do this?
R.M's method with suitable modification:
Flatten @ Partition[list, 3, 5, 1, {}]
{3, 5, 5, 5, 1, 0, 4}
Or for recent versions of Mathematica using UpTo
Flatten @ Partition[list, UpTo[3], 5]
Flatten[Take[#, UpTo[3]] & /@ Partition[list, 5, 5, 1, {}]]
{3, 5, 5, 5, 1, 0, 4}
Flatten[Take[#,UpTo[3]]&/@Partition[list,UpTo[5]]].
– Fred Simons
Mar 08 '17 at 18:50
Flatten[Map[list[[# ;; ;; 5]] &, {1, 2, 3}], {2, 1}]
{3, 5, 5, 5, 1, 0, 4}
Partitioncan easily do this.. – yode Mar 08 '17 at 20:02UpTo[3]instead of3slows down the solution with a factor 8 / 10. Therefore, for very long lists @rcollyer's solutionPick[list,PadRight[#,Length@list,#]&@UnitStep@Range[2,-2,-1],1]or this variantPick[list, Clip[Mod[Range[Length[list]], 5], {2,3}], 2]could be considered as well. – Fred Simons Mar 14 '17 at 19:28Partitionslows it down to begin with, i.e.{}is slower than doing aPadRighton the original list then trimming after. I cannot remember where this was discussed but a search would probably find it. – Mr.Wizard Mar 15 '17 at 06:34