I saw this question,and pop up this idea.
Is there an efficient way to do this in PHP?
EDIT
Best with a demo?
You could use the pear package Math_Matrix for this.
This package claims to be able to do what you are looking for.