3
$\begingroup$

I would like to gather parts of a list and compile them to a new list. for example the list is: {1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5...}. I need to able to take the 3rd through the 5th for each value such that the new list will be {1,1,1,2,2,2,3,3,3,...}.

My data out put is setup that the first column is time, second is wavelength, third is signal data. Its setup that it takes one spectrum at every time point, so the first column ends up being the same time value for the entire wavelength portion. I only need a portion of the spectrum, so I need to only select the cells I need.

What I've tried:data[[25;;50, 1]] resulting in only pulling the first set up time points from 25 to 50. I can't figure out how to get the 2nd, 3rd,... time point. The wavelength section has 2048 entries, so my first guess would be to do something like: t1 = Downsample[data[[1, 1 ;;, 1]], 2048] n = Range[0, Length[t1]] data[[n*2048+(25;;50),1]]

$\endgroup$
3
  • 2
    $\begingroup$ list // Partition[#, 5] & // Map[Drop[#, -2] &] // Flatten $\endgroup$ Apr 17, 2021 at 4:14
  • 2
    $\begingroup$ list = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5};Join @@ Partition[list, 5][[All, 3 ;; 5]]? $\endgroup$
    – kglr
    Apr 17, 2021 at 4:17
  • 3
    $\begingroup$ .. or Join @@ Split[list][[All, 3 ;; 5]]? $\endgroup$
    – kglr
    Apr 17, 2021 at 4:18

3 Answers 3

2
$\begingroup$
list = {1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5};

SequenceCases[list, {a_, a_ ,a_ ,_ ,_} -> Sequence[a, a, a]]

{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5}

$\endgroup$
2
$\begingroup$
list = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5};

Splice@#[[;; 3]] & @@@ Outer[List, Split[list], 1]

(*{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5}*)
$\endgroup$
0
$\begingroup$

For

list = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5,
5, 5, 5, 5};

you can use Split and Catenate

Catenate[#[[3 ;; 5]] & /@ Split[list]]

yields: {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5}

$\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.