working matrix generator function