Section10.2Multiplication, Division, and Exponentiation with Arrays
If we want to perform element-by-element multiplication (or division, or exponentiation) on arrays it is almost identical to addition or subtraction, with just one tiny difference. What I mean by element-by-element multiplication would look like this in the general case:
So if we multiply a 2x2 matrix \([A]\) with a 2x2 matrix \([B]\text{,}\) the output is the multiplication of the corresponding elements. Hopefully, you still have the 4 variables still stored in your Workspace.
Before continuing, THINK about multiplying mat1*mat2 and storing in a new matrix called huh. What should the value of huh(1,1) be? Do not skip your brain workout! It only takes one second and will make the following example much more compelling.
So did we break MATLAB? Is MATLAB incapable of doing such a simple mathematical operation? The value of mat1(1,1) is \(4\) and the value of mat2(1,1) is \(-10\) so \(4*-10\) should equal \(-40\) so why is the number \(20\) showing up in huh(1,1)?
Donβt worry, MATLAB is NOT broken. It is simply doing a different type of math than what you are expecting. Iβll explain what is happening in a bit, but for now, try the exact same operation, but this time, add a . before the * and letβs store the result in a new matrix called oh_okay. Type the following into the command window:
Remember that quick note at the beginning of the chapter? The reason you need to add the . for multiplication is to signify element-by-element operations. The * is reserved for linear algebra multiplication (as are the / and ^ symbols) so we need to specify the . to let MATLAB know we want to perform element-by-element operations.
That means that unless you are specifically doing linear algebra, you will want to put a . before you perform multiplication, division, or exponentiation with arrays. Do not indiscriminately add dots to your equations! Figure 10.2.5 below shows the only times that you need dots.