[Numpy-discussion] Initialization of array?

Chris Barker cbarker at jps.net
Wed Nov 29 14:13:39 EST 2000


Daehyok Shin wrote:
> When I initialize an array, I use
> a = ones(shape)*initial_val
> 
> But, I am wondering if Numpy has more efficient way.
> For example,
> a = array(initial_value, shape)

I don't know if it's any more efficient (what you have is pretty fast
already), but another option is to use resize:

>>> shape = (3,4)

>>> initial_val = 5.0

>>> resize(initial_val,shape)

array([[ 5.,  5.,  5.,  5.],
       [ 5.,  5.,  5.,  5.],
       [ 5.,  5.,  5.,  5.]])
>>>           

-Chris


-- 
Christopher Barker,
Ph.D.                                                           
cbarker at jps.net                      ---           ---           ---
http://www.jps.net/cbarker          -----@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Water Resources Engineering       ------   @    ------   @   ------   @
Coastal and Fluvial Hydrodynamics -------      ---------     --------    
------------------------------------------------------------------------
------------------------------------------------------------------------



More information about the NumPy-Discussion mailing list