Given a single start and stop,
numpy.arange is a good solution for building a NumPy array of evenly spaced values. However, given an array of start and an array of stop values, I would like to build an array of concatenated evenly spaced values, and do so in C speed (no looping). Here is my current solution, though I am wondering if there is a NumPy/SciPy function I missed that already does this.
def vrange(starts, lengths): """ Create concatenated ranges of integers for multiple start/length Args: starts (numpy.array): starts for each range lengths (numpy.array): lengths for each range (same length as starts) Returns: numpy.array: concatenated ranges See the following illustrative example: starts = np.array([1, 3, 4, 6]) lengths = np.array([0, 2, 3, 0]) print vrange(starts, lengths) >>> [3 4 4 5 6] """ # Repeat start position index length times and concatenate cat_start = np.repeat(starts, lengths) # Create group counter that resets for each start/length cat_counter = np.arange(lengths.sum()) - np.repeat(lengths.cumsum() - lengths, lengths) # Add group counter to group specific starts cat_range = cat_start + cat_counter return cat_range
If you are curious why I need this, it's for building a 1-to-many mapping of intervals to contained positions.