Is it an idea to create separate functions for each matrix creation? They almost look like as divided into subfunctions now.

Making that division it also becomes easier to unit-test the sub-modules, and imho, it improves the readability as well.