[Numpy-discussion] empty data matrix (are they really empty ?)

Christopher Barker Chris.Barker at noaa.gov
Thu Dec 14 11:40:43 CST 2006


Sven Schreiber wrote:

>> In the old file I created a matrix  on the fly. I know that Numpy  and 
>> python cannot do that so I found a workaround

numpy can create matrices on the fly, in fact, you are doing  that with 
this code! The only thing it doesn't do is have a lateral that joins 
matrices the way matlab does -- you need to use vstack and the like.

>> First I create the empty matrix

To get better performance, you could create the entire empty matrix, not 
just one row -- this is the same as MATLAB -- if you know how big your 
matrix is going to be, it's better to create it first with "zeros". In 
numpy you can use either zeros or empty - just make sure that if you use 
empty, you fill the whole thing later, or you'll get garbage.

Your code:
lev2=empty((1,h))
# you've just created and empty single row
.
.
.
   lev2=vstack((lev2,clev))
#now you are creating a whole new array, with one more row than before.

The alternative:

lev2=empty((nstep+1,h)) 3 create the whole empty array
ir=1
for j in arange(1,nstep+2):
   a2=gr[arange(ir-1,ir+nstep)]
   clev=diag(dot(a2,dot(disper,a2.transpose())))
   lev2[j,:] = clev # fill in the row you've just calculated
   ir=ir+nstep+1
print lev2

I may have got some of the indexing wrong, but I hope you get the idea.

By the way, if you sent a complete, runnable sample, we can test out 
suggestions, and you'll get better answers.

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov


More information about the Numpy-discussion mailing list