implement dynamic matrix construction