Skip to main content
Code Review

Return to Question

Post Reopened by Community Bot, TheCoffeeCup, rolfl
added 646 characters in body
Source Link
andrew
  • 225
  • 3
  • 7

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 importvrange(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 npstarts)
>>>
>>> start Returns:
 numpy.array: concatenated ranges
 See the following illustrative example:
 starts = np.array([1, 3, 4, 6])
>>> stop lengths = np.array([1[0, 52, 73, 6]0])
>>>
>>> sizes = stop - start print vrange(starts, lengths)
>>> [3 4 4 5 6]
>>> starts """
 
  # Repeat start position index length times and concatenate
 cat_start = np.repeat(startstarts, sizeslengths)
>>> # Create group counter that resets for each start/length
 cat_counter = np.arange(sizeslengths.sum()) - np.repeat(sizeslengths.cumsum() - sizeslengths, sizeslengths)
>>>
>>> ranges = starts +# Add group counter
>>> to group specific starts
>>> print ranges cat_range = cat_start + cat_counter
[3 4 4 5 6]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.

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.

>>> import numpy as np
>>>
>>> start = np.array([1, 3, 4, 6])
>>> stop = np.array([1, 5, 7, 6])
>>>
>>> sizes = stop - start
>>>
>>> starts = np.repeat(start, sizes)
>>> counter = np.arange(sizes.sum()) - np.repeat(sizes.cumsum() - sizes, sizes)
>>>
>>> ranges = starts + counter
>>>
>>> print ranges
[3 4 4 5 6]

If you are curious why I need this, it's for building a 1-to-many mapping of intervals to contained positions.

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.

Post Closed as "Not suitable for this site" by Gareth Rees, Abbas, rolfl
deleted 5 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Given a single start and stop, numpy.arangenumpy.arange is a good solution for building a numpyNumPy array of evenly spaced values. However, given an array of start and an array of stop values, iI would like to build an array of concatenated evenly spaced values, and do so in cC speed (no looping). Here is my current solution, though iI am wondering if there is a numpyNumPy/scipySciPy function iI missed that already does this.

>>> import numpy as np
>>>
>>> start = np.array([1, 3, 4, 6])
>>> stop = np.array([1, 5, 7, 6])
>>>
>>> sizes = stop - start
>>>
>>> starts = np.repeat(start, sizes)
>>> counter = np.arange(sizes.sum()) - np.repeat(sizes.cumsum() - sizes, sizes)
>>>
>>> ranges = starts + counter
>>>
>>> print ranges
[3 4 4 5 6]

If you are curious why iI need this, itsit's for building a 1 to many-to-many mapping of intervals to contained positions.

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.

>>> import numpy as np
>>>
>>> start = np.array([1, 3, 4, 6])
>>> stop = np.array([1, 5, 7, 6])
>>>
>>> sizes = stop - start
>>>
>>> starts = np.repeat(start, sizes)
>>> counter = np.arange(sizes.sum()) - np.repeat(sizes.cumsum() - sizes, sizes)
>>>
>>> ranges = starts + counter
>>>
>>> print ranges
[3 4 4 5 6]

If you are curious why i need this, its for building a 1 to many mapping of intervals to contained positions.

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.

>>> import numpy as np
>>>
>>> start = np.array([1, 3, 4, 6])
>>> stop = np.array([1, 5, 7, 6])
>>>
>>> sizes = stop - start
>>>
>>> starts = np.repeat(start, sizes)
>>> counter = np.arange(sizes.sum()) - np.repeat(sizes.cumsum() - sizes, sizes)
>>>
>>> ranges = starts + counter
>>>
>>> print ranges
[3 4 4 5 6]

If you are curious why I need this, it's for building a 1-to-many mapping of intervals to contained positions.

Source Link
andrew
  • 225
  • 3
  • 7

Vectorized numpy version of arange with multiple start stop

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.

>>> import numpy as np
>>>
>>> start = np.array([1, 3, 4, 6])
>>> stop = np.array([1, 5, 7, 6])
>>>
>>> sizes = stop - start
>>>
>>> starts = np.repeat(start, sizes)
>>> counter = np.arange(sizes.sum()) - np.repeat(sizes.cumsum() - sizes, sizes)
>>>
>>> ranges = starts + counter
>>>
>>> print ranges
[3 4 4 5 6]

If you are curious why i need this, its for building a 1 to many mapping of intervals to contained positions.

lang-py

AltStyle によって変換されたページ (->オリジナル) /