Numeric python question: How do I zero all elements of a matrix 0 ?

Carl Banks imbosol at
Tue Oct 15 01:28:12 CEST 2002

Ram Bhamidipaty wrote:
> I have a matrix created with the Numeric package,
> I want to keep reusing the same matrix, in order to
> do this I need to zero out all the elements.
> I know I could write a loop like this:
> # my arrays are two dimensional
> t = M.shape
> for x in range(0,t[0]):
>   for y in range(0,t[1]):
>      M[x,y] = 0.0
> But I have a feeling that this would be very slow. Is
> there a special function that I can use to do this?

M[:] = 0.0


More information about the Python-list mailing list