[Numpy-discussion] assigning to submatrix
Perry Greenfield
perry at stsci.edu
Tue Jul 29 13:24:10 CDT 2003
> 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
>
At least one way, but it isn't particularly convenient or
pretty (though the ugly details could be hidden in a function).
(and I warn you, I haven't tested this, but something like
this ought to work)
# produce all possible combinations of rind and cind as
# indices into an equivalent flattened for a
indices = add.outer(rind*a.shape[1],cind).flat
put(a, indices, b.flat)
One can do this for numarray as well in a bit more straightforward
way (depending on your view) [again, untested]
t = ones(b.shape)
a[multiply.outer(rind,t[0]),cind*t] = b
In other words, this constructs two index arrays, one for each
dimension, each with the same shape as the source array, b where
the locations in that array correspond to the desired index
in the source array. For numeric, you must do all the selection
on a 1-d array, hence the index arithmetic.
Perry
More information about the Numpy-discussion
mailing list