> On Thu, Jul 3, 2008 at 6:57 AM, Brain Stormer <[EMAIL PROTECTED]> wrote: >> 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)
Brain, I would avoid calling your variable matrix since it collides with numpy.matrix class, which you've imported with *. The topic to look up in the documentation is called "slicing". You'll find good explanations here. http://www.scipy.org/NumPy_for_Matlab_Users http://www.scipy.org/Tentative_NumPy_Tutorial http://www.tramy.us/ (Guide to NumPy) Try to vectorize (avoid for loops) your code whenever possible. When for loops are needed, use xrange for large loops. I hope this helps. :-) Damian >> 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]] > >>> matrix[fillxstart:fillxstop, fillystart:fillystop] = 1 >>> matrix > > array([[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]]) > _______________________________________________ > Numpy-discussion mailing list > [email protected] > http://projects.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ Numpy-discussion mailing list [email protected] http://projects.scipy.org/mailman/listinfo/numpy-discussion
