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_n# 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 #A# and #B#, where #A# is a matrix with #m# rows and #n# columns and #B# is a matrix with #n# rows and #p# columns, the resultant matrix #AB# will possess #m# rows and #p# columns. The multiplication cannot occur at all if the number of columns in #A# is not equal to the number of rows in #B#, so if #AB# exists, the only way for #BA# to exist at all would be if #p=m#, thus making matrix #A# a #m#x#n# matrix and matrix #B# a #n#x#m# matrix. However, if #p=n#, it is still quite possible for #AB != BA#
Recall how each entry in the matrix product is determined. When matrices #A# and #B# are multiplied into matrix #AB#, each entry in the new matrix is formed from the entries in the old matrix. Specifically, to find matrix entry #AB_(ij)# (that is, the entry in the #i# row and #j# column of matrix #AB#), we take the dot product of row #i# of matrix #A#, and column #j# of matrix #B#.
#AB_(ij) = sum_(k=1)^n A_(ik) B_(kj)#
As an example, consider the 3x3 matrices #A# and #B#, 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.