Once you have entered two matrices--that is, have given them names in Matlab--all you need to do to multiply them is to enter the name of the first factor, followed by the multiplication sign * (the asterisk) followed by the name of the second factor.
Thus, the first multiplication in the Examples section is done by entering three commands, first A = [1 2; 3 4] then B = [-1 1; 2 -2] then A*B. Matlab will show the product matrix. (Note that the product of the same two matrices in the opposite order, which is also given on page 7, can be computed simply by entering the fourth command B*A.)
You will have noticed that Matlab shows the product matrix preceded by `ans ='. This indicates that if you want to refer to the result of the computation just done, you can do so using the name `ans'. This name is temporary, of course, because as soon as you do another computation, the name `ans' will then refer to the result of the new computation. If you want to retain the result of the computation A*B for later use, you need to give it a name. For example, you could give the command D = ans to give it the name D for later use. But, if you knew at the outset that you wanted to retain the result of the computation, you could have entered D = A*B, instead of just A*B, to give the result the name D, rather than the temporary name `ans'. (Doing this also has the effect of leaving the meaning of `ans', if it has one, unchanged.)
To test the case of the associative law in the equation at the bottom of page 7, after the matrices A and B have been entered as above, enter the commands C = [-1; 2], D = A*B, E = B*C, D*C, A*E. The matrices produced by the last two commands will be the same. An easy way to check that they are the same is to enter the command D*C - A*E and see that the resulting matrix has all entries equal to zero.
Another way to test the associative law is to compare the results of the commands (A*B)*C and A*(B*C). Matlab of course takes the associative law for granted and requires no parentheses, so you can simply enter the command A*B*C to get the same result.
Return to Chapter 1.