Choose m evenly spaced elements from a sequence of length n

后端 未结 5 1465
再見小時候
再見小時候 2020-12-09 12:24

I have a vector/array of n elements. I want to choose m elements.

The choices must be fair / deterministic -- equally many from each subsection.

With m=10, n

5条回答
  •  Happy的楠姐
    2020-12-09 13:20

    I am working on a clinical application and found all answers above to have different degrees of bias. Here's another solution that works well even in a circle. That is, even if the last number wraps around as in when you work with degrees 0° = 360°.

    import numpy as np
    m = 51
    # Generate intervals
    epts = np.linspace(0,360,m+1,endpoint=True)
    # Create the halfsteps between intervals (One would have sufficed)
    halfsteps = (epts[1:] - epts[:-1]) / 2
    # Find the midpoints
    midpoints = epts[:-1] + halfsteps
    # Make an unbiased rounding
    results = np.around(midpoints, decimals=0)
    

提交回复
热议问题