[Numpy-discussion] two dimensional array of sets

David Warde-Farley dwf at cs.toronto.edu
Thu Aug 12 17:36:03 EDT 2010


On 2010-08-12, at 3:59 PM, gerardob wrote:

> 
> Hello, this is a very basic question but i don't know the answer.
> 
> I would like to construct a two dimensional array A, such that A[i][j]
> contains a set of numbers associated to the pair (i,j). For example, A[i][j]
> can be all the numbers that are written as i^n*j*5-n for some all n=1,..,5
> 
> Is numpy what i need? if not which is the way to define such arrays. I mean,
> how do i declare an 2-dimensional array of a given size (for after start
> filling it with specific sets)?

The easiest way to do this would be

>>> import numpy as np
>>> n = 2
>>> i, j = np.ogrid[1:6, 1:6]
>>> arr = i**n * j*5 - n
>>> print arr
array([[  3,   8,  13,  18,  23],
       [ 18,  38,  58,  78,  98],
       [ 43,  88, 133, 178, 223],
       [ 78, 158, 238, 318, 398],
       [123, 248, 373, 498, 623]])

David


More information about the NumPy-Discussion mailing list