Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here's another way to do it:

def f(elt):
    return matrix([elt for j in range(3)])

block_matrix([map(f,row) for row in M])

However, from testing with %%timeit on random 3x3 matrices (random_matrix(RDF,3,3)) it seems that this is always very slightly slower than your list constructor approach:

your approach:

1000 loops, best of 3: 932 µs per loop

my above approach:

1000 loops, best of 3: 958 µs per loop

I also tried using M.apply_map(f) but it seems that that function assumes that f returns a scalar element and I couldn't get it to work.