Putting together a larger matrix from smaller matrices

Peter Otten __peter__ at web.de
Tue Aug 25 04:03:57 EDT 2009


Matjaz Bezovnik wrote:

> This is something which is done often in FEM methods, and the alike.
> 
> I have matrix A of 3x3 elements, and B, of the same number of
> elements, 3x3.
> 
> What would be the most obvious way to assemble a matrix which:
> a11 a12 a13
> a21 a22 a23
> a31 a32 a33+b11 b12 b13
>                       b21 b22 b23
>                       b31 b32 b33
> 
> (the missing elements = zero)
> 
> (you see the pattern - if there was a third matrix C, it would
> "connect" to b33 on the main diagonal)

Unless there is a dedicated function:

partial = [...] # list of matrices of shape NxN

N = partial[0].shape[0]
width = N*len(partial)
b = numpy.zeros((width, width))

off = 0
for m in partial:
    b[off:off+N, off:off+N] = m
    off += N
print b

Peter





More information about the Python-list mailing list