![](https://secure.gravatar.com/avatar/85c39b948d14e7448ca13b349097b5c0.jpg?s=120&d=mm&r=g)
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)? Thanks. -- View this message in context: http://old.nabble.com/two-dimensional-array-of-sets-tp29422693p29422693.html Sent from the Numpy-discussion mailing list archive at Nabble.com.
![](https://secure.gravatar.com/avatar/b684c02bab6c8d54c0c25c4b69ee1135.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/85c39b948d14e7448ca13b349097b5c0.jpg?s=120&d=mm&r=g)
As i wrote, the elements of each A[i][j] are sets and not numbers. David Warde-Farley-2 wrote:
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 _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- View this message in context: http://old.nabble.com/two-dimensional-array-of-sets-tp29422693p29423645.html Sent from the Numpy-discussion mailing list archive at Nabble.com.
![](https://secure.gravatar.com/avatar/b684c02bab6c8d54c0c25c4b69ee1135.jpg?s=120&d=mm&r=g)
On 2010-08-12, at 5:54 PM, gerardob wrote:
As i wrote, the elements of each A[i][j] are sets and not numbers.
Ah, missed that part. Assuming you don't need/want these sets to be mutable or differently sized, you could do something like this. Here we make 'n' an array of the numbers 0 to 49, but you could do other things. We immediately reshape it to a 3D array with the 50 elements being the "depth" dimension.
n = np.arange(50).reshape(1, 1, 50) i, j, _ = np.ogrid[1:6, 1:6, 0:1] # adding a dummy dimension
Look in the NumPy docs about "broadcasting" to see why this works the way it does, also have a gander at the contents of the i and j arrays and their .shape properties.
arr = i**n * j*5 - n print arr[0, 0]
[ 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44] If you want the sets to be proper Python sets then you might want to look at Ben's reply, but you can accomplish many operations on sets using 1-d NumPy arrays. Have a look at help(np.lib.arraysetops) for a list of functions. Also, NumPy can only work with fixed-size data types so if you need to work with numbers bigger than 2^63 - 1 (or smaller than -2^63) than you ought to use Ben's method. David David
![](https://secure.gravatar.com/avatar/b0714eded10b5ccee614c7134360974b.jpg?s=120&d=mm&r=g)
Hi, You can use np.mgrid to construct a grid of coordinates. From there, you can make your new array based on these coordinates however you like. Ian
![](https://secure.gravatar.com/avatar/09939f25b639512a537ce2c90f77f958.jpg?s=120&d=mm&r=g)
On Thu, Aug 12, 2010 at 2:59 PM, gerardob <gberbeglia@gmail.com> 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)?
Thanks.
Because you want to hold a set, it might possibly be better to use python
lists and list comprehensions. However, you can use numpy to help you out... import numpy as np rows = 5 cols = 5 n = np.arange(1, 6) a = [[set((i**n) * (j*(5 - n))) for j in range(1, cols+1)] for i in range(1,rows+1)] The first row is a[0] (which is i == 1) : [set([0, 1, 2, 3, 4]), set([8, 0, 2, 4, 6]), set([0, 9, 3, 12, 6]), set([16, 8, 12, 4, 0]), set([0, 10, 20, 5, 15])] I hope this helps, Ben Root
participants (4)
-
Benjamin Root
-
David Warde-Farley
-
gerardob
-
Ian Mallett