matrices generation code