Created
July 8, 2019 08:47
-
-
Save freifrauvonbleifrei/d3774f00e12572d424ac15d2f6166a43 to your computer and use it in GitHub Desktop.
Python numpy: get all combinations of d ints within certain bounds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def bound_combinations(lmin, lmax): | |
""" | |
example: bound_combinations([1,1], [2,3]) == [[1,1], [1,2], [1,3], [2,1], [2,2], [2,3]] | |
""" | |
assert len(lmax) == len(lmin) | |
d = len(lmax) | |
# get all the ranges as slices | |
ranges = tuple(slice(lmin[i],lmax[i]+1, 1) for i in range(llen)) | |
# get all combinations and reshape accordingly | |
return np.stack(np.mgrid[ranges], axis=llen).reshape((-1,llen)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment