In general, matrix multiplication is not commutative. There are some exceptions, however, most notably the identity matrices (that is, the n by n matrices I_nIn which consist of 1s along the main diagonal and 0 for all other entries, and which act as the multiplicative identity for matrices)
In general, when taking the product of two matrices AA and BB, where AA is a matrix with mm rows and nn columns and BB is a matrix with nn rows and pp columns, the resultant matrix ABAB will possess mm rows and pp columns. The multiplication cannot occur at all if the number of columns in AA is not equal to the number of rows in BB, so if ABAB exists, the only way for BABA to exist at all would be if p=mp=m, thus making matrix AA a mmxnn matrix and matrix BB a nnxmm matrix. However, if p=np=n, it is still quite possible for AB != BAAB≠BA
Recall how each entry in the matrix product is determined. When matrices AA and BB are multiplied into matrix ABAB, each entry in the new matrix is formed from the entries in the old matrix. Specifically, to find matrix entry AB_(ij)ABij (that is, the entry in the ii row and jj column of matrix ABAB), we take the dot product of row ii of matrix AA, and column jj of matrix BB.
AB_(ij) = sum_(k=1)^n A_(ik) B_(kj)ABij=n∑k=1AikBkj
As an example, consider the 3x3 matrices AA and BB, with
A = ((a_(11),a_(12),a_(13)),(a_(21),a_(22),a_(23)),(a_(31),a_(32),a_(33))) and B = ((b_(11), b_(12), b_(13)),(b_(21), b_(22), b_(23)),(b_(31), b_(32), b_(33))).
Then AB_(2,3) is simply the dot product of the second row of A and the third column of B, or (a_(21)\ a_(22)\ a_(23))\ .\ ((b_(13)),(b_(23)),(b_(33))) = (a_(21)*b_(13)) + (a_(22)*b_(23)) + (a_(23)*b_(33))
However, BA_(2,3) would be the dot product of row 2 of matrix B and column 3 of matrix A, or...
(b_(21)\ b_(22)\ b_(23))\ .\ ((a_(13)),(a_(23)),(a_(33))) = (b_(21)*a_(13)) + (b_(22)* a_(23))+(b_(23)*a_(33))
If matrix multiplication were commutative, we would expect BA_(2,3) = AB_(2,3) (among other things). Since these expressions may very well not be equal according to our work thus far, we can safely conclude that matrix multiplication is not necessarily commutative.