There are two signs: \(*\) and %*% , both means multiplication in broad terms, but they are different when dealing with matrices.
Suppose I have two vectors:
A <- matrix (1:6, nrow = 2)
B <- matrix (5:10, ncol = 2)
View these matrices:
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [,1] [,2]
## [1,] 5 8
## [2,] 6 9
## [3,] 7 10
Generally, when we want matrix multiplication
for example, the resulting matrix should have [1,1] as:
\[ 1 \times 5 + 3 \times 6 + 5 \times 7 = 58\] or the [2,2] position be: \[ 2\times 8 + 4 \times 9 + 6 \times 10 = 112\]
We can do this by using the symbol: %*%
A %*% B
## [,1] [,2]
## [1,] 58 85
## [2,] 76 112
Then what about the symbol: \(*\)
We can create another two column matrices, C and D:
C <- matrix (1:4, ncol = 1)
D <- matrix (5:8, ncol = 1)
View C and D:
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [,1]
## [1,] 5
## [2,] 6
## [3,] 7
## [4,] 8
Let’s try to use \(*\)
C * D
## [,1]
## [1,] 5
## [2,] 12
## [3,] 21
## [4,] 32
As can be seen, this \(*\) symbol can be regarded as: computing the dot product
while for %*% it means: give me the cross product