MATLAB Inner Product Guide: Syntax, Functions, and Practical Examples
The MATLAB inner product is a foundational operation for measuring similarity, projecting vectors, and building the matrix calculations that power engineering and data analysis workflows. Whether you are comparing sensor signals, computing correlation, solving least-squares problems, or implementing a machine learning algorithm, the inner product appears again and again. MATLAB gives you several ways to compute it, from the concise dot function to element-wise multiplication with sum and matrix multiplication with the transpose operator.
At first glance, the inner product can seem trivial: multiply corresponding entries and add the results. In practice, MATLAB users need to pay attention to vector orientation, complex conjugation, array dimensions, and performance. Understanding those details helps you avoid subtle bugs, especially when your data contains complex numbers or when you are working with matrices instead of simple one-dimensional vectors.
MATLAB Inner Product Fundamentals
For two real vectors, the MATLAB inner product is the sum of the products of their corresponding elements. If a = [1 2 3] and b = [4 5 6], then the inner product is 1*4 + 2*5 + 3*6 = 32. MATLAB computes this directly with dot(a,b). For complex vectors, the function conjugates the first input, so dot(a,b) is equivalent to sum(conj(a).*b).
The result of a vector inner product is a scalar. That scalar encodes useful geometric information: it is related to the lengths of the vectors and the angle between them. When the inner product is zero, the vectors are orthogonal. When the vectors are normalized to unit length, the inner product equals the cosine of the angle between them. These properties make the MATLAB inner product essential in linear algebra, signal processing, and statistics.
Using the dot Function with Vectors
The most direct syntax is dot(a,b), where a and b are vectors of the same length. MATLAB handles row vectors, column vectors, and mixed orientation in many common cases, but using a consistent orientation reduces confusion. For example, if a = [1 2 3] and b = [4 5 6], then dot(a,b) returns 32. If a = [1; 2; 3] and b = [4; 5; 6], the result is still 32 because the operation pairs elements by position.
When complex numbers are involved, remember that dot(a,b) uses the complex conjugate of a. So if a = [1+2i, 3] and b = [4, 5-1i], MATLAB computes conj(a(1))*b(1) + conj(a(2))*b(2). This is the standard Hermitian inner product used in quantum mechanics, communications, and advanced linear algebra. If you want a non-conjugating bilinear product instead, you need to compute it manually with sum(a.*b).
Manual Computation with Element-wise Multiplication and Sum
You can always reproduce the MATLAB inner product manually by combining element-wise multiplication and summation. For real vectors, the expression sum(a.*b) returns the same scalar as dot(a,b). For complex vectors, use sum(conj(a).*b) to match the behavior of the dot function. This manual approach is helpful when you need to apply weights, masks, or custom rules to specific elements before summing.
For matrices and multidimensional arrays, manual computation gives you explicit control over the reduction dimension. If A = [1 2; 3 4] and B = [5 6; 7 8], then sum(A.*B,1) returns [26 44], which are the inner products of the columns. sum(A.*B,2) returns [17; 53], which are the inner products of the rows. The dot function offers similar dimension control through dot(A,B,dim).
Advanced MATLAB Inner Product Patterns and Applications
Matrix multiplication in MATLAB can be understood as a large collection of inner products. For C = A*B, each entry C(i,j) is the product of row i of A and column j of B. This operation is implemented with highly optimized BLAS routines, so it is usually much faster than writing explicit loops. However, MATLAB's matrix multiplication does not conjugate either input, so it is not always the Hermitian inner product for complex data.
When you need the standard complex inner product between columns, use the conjugate transpose operator, which is written as an apostrophe in MATLAB. The expression A'*B conjugates the rows of A and then multiplies by B, producing a matrix whose entries are inner products of columns. This pattern appears in covariance matrices, Gram matrices, least-squares normal equations, and principal component analysis.
Matrix Multiplication, Gram Matrices, and Complex Conjugation
If X is a data matrix whose columns are variables or observations, then X'*X computes a Gram matrix. Each entry (i,j) is the inner product of column i and column j of X. For real data, this matrix is symmetric. For complex data, it is Hermitian because the apostrophe performs a conjugate transpose. This distinction is critical: using X.'*X instead of X'*X removes the conjugation and changes the mathematical meaning of the result.
The dot function connects directly to these matrix expressions. For example, dot(X(:,i),X(:,j)) produces the same scalar as X(:,i)'*X(:,j). If you need to compute many inner products at once, matrix multiplication is often the cleanest and fastest route. If you need to compute inner products along a specific dimension of a multidimensional array, dot or sum(conj(A).*B,dim) gives you precise control.
Norms, Angles, and Orthogonal Projections
The MATLAB inner product is closely tied to vector norms. For a real vector v, norm(v) equals sqrt(dot(v,v)). For a complex vector, dot(v,v) equals the sum of squared magnitudes, so the same square root formula produces the Euclidean norm. You can compute the angle between two vectors with acos(dot(u,v)/(norm(u)*norm(v))), assuming neither vector is zero. This formula is widely used in similarity scoring, directional statistics, and computer vision.
Orthogonal projection also relies on the inner product. To project vector u onto vector v, use (dot(v,u)/dot(v,v))*v for real vectors. For complex vectors, the order of arguments matters because dot conjugates its first input. Two vectors are orthogonal when their inner product is zero, but in floating-point arithmetic you should compare the absolute value against a small tolerance rather than testing for exact zero. These patterns are the building blocks for least-squares fitting, QR decomposition, and subspace methods.
Once you are comfortable with dot, sum(conj(a).*b), and matrix products such as X'*X, you will start recognizing inner products throughout MATLAB code and mathematical models. Try rewriting a few loops as vectorized inner-product expressions, and you will often gain both clarity and speed. The next time you compare signals, measure similarity, or build a projection, choose the MATLAB inner product form that best matches your data type, dimensions, and performance needs.