upgrade matrix function