[Numpy-discussion] assigning to submatrix
John Hunter
jdhunter at ace.bsd.uchicago.edu
Tue Jul 29 12:58:03 CDT 2003
>>>>> "Perry" == Perry Greenfield <perry at stsci.edu> writes:
Perry> [Note that I reordered the index arrays to match b; I
Perry> wasn't sure what was intended]
Yep, I think that was a blooper on my part; to be precise, here is the
matlab code
a = zeros( 8,8 );
b = ones( 3,4 );
rind = [2,4,6];
cind = [1,4,5,6];
a(rind, cind) = b;
and the equivalent python
from Numeric import *
a = zeros( (8,8) )
b = ones( (3,4) )
rind = [1,3,5]
cind = [0,3,4,5]
for ia, ib in zip(rind, range(len(rind))):
for ja, jb in zip(cind, range(len(cind))):
a[ia,ja] = b[ib,jb]
print a
And the question is: is there a good way to avoid the double loop
using Numeric?
Thanks,
John
More information about the Numpy-discussion
mailing list