4
$\begingroup$

I have a list with 30000 plus elements. I'd like to sample 500 points from that list in an even manner, into another list.

More, ideally I'd like to compress the 30000 into a 500 long list by averaging in 30000/500=60 points at a time. I.e. an element of the end list is a Mean[] of 60 elements on the first list.

I can construct an expressions to do both options, but I'd like to know if Mathematica has something in stock. This is hugely useful for signal processing, hence my expectation regarding Mathematica's functions.

$\endgroup$
6
$\begingroup$

Let's generate a random list of reals:

list = RandomReal[{-100, 100}, 30000];

There is Mean which can do what you expect. i.e. every consecutive 60 elements is averaged:

Partition[ list, 500] // Transpose // Mean
| improve this answer | |
$\endgroup$
  • $\begingroup$ I knew it. There is something hugely useful in Mathematica for this. $\endgroup$ – A. Vieira Sep 7 '17 at 17:09
  • $\begingroup$ And if I want only the last element I can easily take the last element of all partitions with Partition[list, 500][[All,500]]. $\endgroup$ – A. Vieira Sep 7 '17 at 17:12
  • $\begingroup$ The above is the most straightforward approach, in case of expecting something more involved one should first mention about known methods in the OP, however I believe there is no need for complicating life. $\endgroup$ – Artes Sep 7 '17 at 17:25
  • 1
    $\begingroup$ @A.Vieira Instead of Partition[list, 500][[All,500]] you can also write Partition[list, 500][[All,-1]] or even Last /@ Partition[list,500] to get the last element of each partition. $\endgroup$ – Thies Heidecke Sep 7 '17 at 17:50
5
$\begingroup$
SeedRandom[1]
list = RandomReal[{-100, 100}, 30000]; 

Developer`PartitionMap[Mean, list, 500]

{-1.55803,2.22818,-0.0508766,3.25136,0.846088,1.94176,-1.73064,1.07554,0.247383,6.93271,2.76486,0.966219,-2.52573,1.78763,-4.1424,-1.30308,0.350464,0.447681,2.12322,-1.69277,1.47585,-0.923214,2.44039,2.92054,1.9305,-1.65154,-1.5551,-2.78769,2.63487,0.458904,3.18371,0.00747208,-1.99041,-3.63672,-4.10655,2.16469,2.63422,0.609351,0.73959,1.46082,-1.55098,-2.46442,-1.34251,-6.38555,3.16249,0.381931,-0.262874,-0.575258,0.745162,-4.29312,-0.168863,-0.275357,-4.23135,1.23062,2.6862,4.61055,2.48747,2.65438,-4.45548,1.09867}

Gives the same result as artes's answer:

% == (Partition[list, 500] // Transpose // Mean)

True

Re: if I want only the last element I can easily take the last element of all partitions with..., you can also use Developer`PartitionMap with Last replacing Mean:

Developer`PartitionMap[Last, list, 500]

{-15.2243, -53.5129, -59.1726, -89.9014, -21.8066, 94.7291, -50.4855, -40.2763,26.8482, -37.5492, -39.7777, 36.555, -35.0392, 69.7234, -47.4006,12.0806, -54.2981, -5.91096, 51.94,11.6728, -39.7397, 61.1442, -11.5572, 44.7992, 55.2541, -12.8397, 76.2317, 72.2243, -46.5035, 3.4014, -99.478, -67.626, 24.6064, 38.3583, 42.6547, 15.6142, 25.748, -90.9753, 97.2486, 66.1974, -61.1141, -78.6101, -60.4224, -53.2592, 42.5433, 8.88568, 33.5063, -99.2404, 97.9869, -5.56118, -67.6508, 92.2843, 40.1395, -18.5641, -93.7015, -40.7611, 85.1767, 82.3689, -89.0389, 18.9333}

Alternatively, you can use Downsample:

Downsample[list, 500, 500] == %

True

| improve this answer | |
$\endgroup$
  • $\begingroup$ Cool, didn't know about PartitionMap! (+1) $\endgroup$ – Thies Heidecke Sep 7 '17 at 17:53

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.