[SciPy-user] Direct sum in numpy/scipy ?
Bill Baxter
wbaxter at gmail.com
Fri May 19 09:41:31 EDT 2006
On 5/19/06, Nils Wagner <nwagner at iam.uni-stuttgart.de> wrote:Hi all,
> How can I build a direct sum of two matrices in numpy/scipy ?
Is there something wrong with just allocating the target matrix and
assigning the parts to it?
Like so:
dsum = num.zeros( num.add(a.shape,b.shape) )
dsum[:a.shape[0],:a.shape[1]]=a
dsum[a.shape[0]:,a.shape[1]:]=b
Full example:
>>> import numpy as num
>>> a=num.arange(0,12).reshape(3,4)
>>> b=num.arange(0,6).reshape(3,2)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> b
array([[0, 1],
[2, 3],
[4, 5]])
>>> dsum = num.zeros( num.add(a.shape,b.shape) )
>>> dsum.shape
(6, 6)
>>> dsum[:a.shape[0],:a.shape[1]]=a
>>> dsum[a.shape[0]:,a.shape[1]:]=b
>>> dsum
array([[ 0, 1, 2, 3, 0, 0],
[ 4, 5, 6, 7, 0, 0],
[ 8, 9, 10, 11, 0, 0],
[ 0, 0, 0, 0, 0, 1],
[ 0, 0, 0, 0, 2, 3],
[ 0, 0, 0, 0, 4, 5]])
Or maybe you wanted a sparse result?
--bb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20060519/9ac0c7cc/attachment.html>
More information about the SciPy-User
mailing list