[Tutor] Array filling

Brain Stormer brnstrmrs at gmail.com
Thu Jul 3 15:53:37 CEST 2008


I am using numpy to create an array then filling some of the values using a
for loop, I was wondering if there is way to easily fill the values without
iterating through sort of like "array.fill[start:stop,start:stop]"?  The
reason for my question is, in some cases, I might have to fill hundreds
(within a 10,000x10,000 matrix) of values and I am not sure if iteration is
the right way to go.

Code:
from numpy import *
x, y = 5, 5
matrix = zeros((y,x), int)
print matrix
fillxstart, fillystart = 1,1
fillxstop,fillystop = 4, 4
for i in range(fillystart,fillystop,1):
    for j in range(fillxstart,fillxstop,1):
        matrix[i,j] = 1
print matrix

Output before filling:
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
Output after filling:
[[0 0 0 0 0]
 [0 1 1 1 0]
 [0 1 1 1 0]
 [0 1 1 1 0]
 [0 0 0 0 0]]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080703/cf4cb5c8/attachment.htm>


More information about the Tutor mailing list