Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Multidimensional Zeros Python

I need to make a multidimensional array of zeros.

For two (D=2) or three (D=3) dimensions, this is easy and I'd use:

a = numpy.zeros(shape=(n,n)) 

or

a = numpy.zeros(shape=(n,n,n))

How for I for higher D, make the array of length n?

like image 806
Sameer Patel Avatar asked Mar 13 '13 18:03

Sameer Patel


1 Answers

You can multiply a tuple (n,) by the number of dimensions you want. e.g.:

>>> import numpy as np
>>> N=2
>>> np.zeros((N,)*1)
array([ 0.,  0.])
>>> np.zeros((N,)*2)
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> np.zeros((N,)*3)
array([[[ 0.,  0.],
        [ 0.,  0.]],

       [[ 0.,  0.],
        [ 0.,  0.]]])
like image 159
mgilson Avatar answered Oct 01 '22 12:10

mgilson