How can I create a numpy matrix with its elements being a function of its indices? For example, a multiplication table: a[i,j] = i*j
a[i,j] = i*j
An Un-numpy and un-
Just wanted to add that @Senderle's response can be generalized for any function and dimension:
dims = (3,3,3) #i,j,k ii = np.indices(dims)
You could then calculate a[i,j,k] = i*j*k as
a[i,j,k] = i*j*k
a = np.prod(ii,axis=0)
or a[i,j,k] = (i-1)*j*k:
a[i,j,k] = (i-1)*j*k
a = (ii[0,...]-1)*ii[1,...]*ii[2,...]
etc