Linear algebra · array operators
Element-by-Element Multiplication of Matrices
Element-by-element multiplication takes two arrays of the same shape and multiplies the entries that sit in the same position. The result has that shape too. Mathematicians call it the Hadamard product or the entrywise product. In MATLAB-style languages it is written with a dot before the star, A.B, to keep it apart from the matrix product AB.
The definition
If A and B are both m × n, then C = A.*B has C(i, j) = A(i, j) · B(i, j) for every i and j. There is no summing across a row, and no requirement that the inner dimensions agree. The only requirement is that the shapes match, or that one of them is a scalar, or that they can be broadcast against each other.
The matrix product is a different animal. For C = A*B, the entry C(i, j) is the sum over k of A(i, k) · B(k, j), so A needs as many columns as B has rows. It mixes entries along rows and columns. The entrywise product mixes nothing.
A worked comparison
Let A = [1 2; 3 4] and B = [5 6; 7 8]. The element-by-element product is
A.*B = [1·5 2·6; 3·7 4·8] = [5 12; 21 32].
The matrix product is built from row-times-column sums. The top-left entry is 1·5 + 2·7 = 19. The top-right is 1·6 + 2·8 = 22. The bottom row gives 3·5 + 4·7 = 43 and 3·6 + 4·8 = 50. So A*B = [19 22; 43 50]. The two answers share the same shape and nothing else.
A = [1 2; 3 4]; B = [5 6; 7 8];
H = A .* B % [5 12; 21 32]
P = A * B % [19 22; 43 50]
Mistaking one for the other is among the most frequent errors in matrix code. It is especially treacherous because it often runs without complaint: when both arrays are square and the same size, both operators are legal and the program just computes the wrong thing.
The dotted family
The dot prefix applies to the whole family of entrywise operations: .* for multiplication, ./ for division and .^ for powers. Addition and subtraction are already entrywise, so they need no dot. Writing x.^2 on a vector squares each entry; leaving the dot out gives a dimension error, because a column vector cannot be multiplied by itself as a matrix.
Mixed with scalars, the rules relax. 3*A scales every entry, and A + 1 adds one to every entry in most languages. Some languages (NumPy among them) make the star itself entrywise and use a separate symbol for the matrix product, which reverses the convention described here. Checking which operator means which in the tool at hand is the first thing to do when moving code between environments.
Where entrywise products turn up
Weights are the common use: multiplying a signal by a window before a transform, as in the Blackman window, is an entrywise product. So is masking, where a matrix of zeros and ones removes unwanted entries, and so is the elementwise step in many gradient formulas. Scaling the rows of a matrix by a vector d can be written with an entrywise product after an expansion of d to the matrix shape, or more cheaply as a diagonal-matrix product.
One identity is worth remembering. The sum of all entries of A.*B equals the trace of AᵀB, which is the Frobenius inner product of the two matrices. In the example above the entries of A.*B sum to 5 + 12 + 21 + 32 = 70. The product AᵀB has diagonal entries 1·5 + 3·7 = 26 and 2·6 + 4·8 = 44, and 26 + 44 = 70. The two agree.
For a different use of matrix operators, see matrix division; for how individual entries are addressed, see elements of a matrix. The linear algebra hub lists the rest.