Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Map a matrix to a block matrix


I have a function that maps an element $x$ into a $1\times n$ matrix, say for example $[x,x,x]$. I would like to map this function to a matrix and consider the resulting matrix as a "block matrix". E.g: change matrix([[1,2],[3,4]]) into matrix([[1,1,1,2,2,2],[3,3,3,4,4,4]]).

For now, I use a trick that basically converts back the matrix to a a list using something like:

block_matrix([[f(elt) for elt in row] for row in M.rows()])

but it looks quite dirty so I would like to know if there are some better way to proceed.

Thank you!