Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numpy mgrid with a variable number of indices

Tags:

python

numpy

How do you use numpy.mgrid with a variable number of indices? I can't find any examples on github of anyone using this with anything but hardcoded values.

import numpy as np
np.mgrid[1:10, 1:10] # this works fine

x = (1, 10)
np.mgrid[x[0]:x[1], x[0]:x[1]] # hardcoded

xs = [(1,10)] * 10
np.mgrid[*xs????] # I can't get anything to work here
like image 427
Charles Offenbacher Avatar asked Nov 20 '25 00:11

Charles Offenbacher


1 Answers

This seems to work:

np.mgrid[[slice(i,j) for i,j in [(1,10)]*10]]

though with *10 it is too large

It's derived from that fact

np.mgrid[slice(1,10),slice(1,10)]  # same as
np.mgrid[1:10,1:10]
like image 118
hpaulj Avatar answered Nov 22 '25 14:11

hpaulj