1

I would like to statically initialize the size of an N by N 2d array in python's numpy where N is a variable (from a SQL query). The equivalent Java would be:

N = code from sql query
int[][] mat = new int[N][N]

How would I do this in numpy? Or what about a Matrix type?

1 Answer 1

2

You can do it in a number of ways, but to create an empty array that you will later fill, you might consider:

N = 100
mat = np.empty((N,N))

There are a large number of other methods detailed in the docs:

http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html

Sign up to request clarification or add additional context in comments.

2 Comments

What is a convenient way to look into the docs? From the interpreter I typed help(numpy) and it kept on spitting out documentation... I eventually had to give the terminate signal...
I would recommend using the IPython interpreter that allows you to do something like np.empty? which will give you that method's specific docs (ipython.org/ipython-doc/dev/interactive/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.