Skip to content

Instantly share code, notes, and snippets.

@OhadRubin
Created December 17, 2023 22:59
Show Gist options
  • Save OhadRubin/64998c97f784b46237b0e1b5323247dd to your computer and use it in GitHub Desktop.
Save OhadRubin/64998c97f784b46237b0e1b5323247dd to your computer and use it in GitHub Desktop.
create_named_matrix
import more_itertools
import itertools
"""
>>> create_named_matrix(2,3,5)
array([[['0,0,0', '0,0,1', '0,0,2', '0,0,3', '0,0,4'],
['0,1,0', '0,1,1', '0,1,2', '0,1,3', '0,1,4'],
['0,2,0', '0,2,1', '0,2,2', '0,2,3', '0,2,4']],
[['1,0,0', '1,0,1', '1,0,2', '1,0,3', '1,0,4'],
['1,1,0', '1,1,1', '1,1,2', '1,1,3', '1,1,4'],
['1,2,0', '1,2,1', '1,2,2', '1,2,3', '1,2,4']]], dtype=object)
"""
def create_named_matrix(*dims):
dim_ranges = [range(i) for i in dims]
matrix = list(itertools.product(*dim_ranges))
matrix = list(map(lambda x: ",".join(map(str,x)),matrix))
matrix = np.array(matrix,dtype=object).reshape(*dims)
return matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment