Mathematical Operations with Arrays
MATLAB Array Arithmetic: Matching Operators to Meaning
MATLAB Array Arithmetic lets you apply a calculation to many measurements at once or combine arrays through linear algebra. These tasks use different operators, even when the inputs look similar. Before writing an expression, decide what each output entry should represent: a calculation on corresponding measurements, or a combination of several inputs. The discussion below assumes ordinary, nonempty real numeric arrays.
Calculate with corresponding entries
An element-wise operation applies an operation separately to corresponding array entries. For equal-sized arrays, element-wise multiplication follows
In MATLAB, write this operation as C = A.*B. The dot belongs to the operator; it tells MATLAB to multiply corresponding entries.
For example, consider these dimensionless arrays:
x = [2 4 6];
y = [3 5 7];
p = x.*y;
q = x.^2;The results are p = [6 20 42] and q = [4 16 36]. Each entry in q is the square of the matching entry in x.
Intended calculation | MATLAB expression | Meaning |
|---|---|---|
Add corresponding entries | A + B | Add each matched pair |
Subtract corresponding entries | A - B | Subtract each matched pair |
Multiply corresponding entries | A.*B | Multiply each matched pair |
Divide corresponding entries | A./B | Divide entries of A by entries of B |
Raise entries to a power | A.^2 | Square each entry of A |
Addition and subtraction use + and - without dots. For ordinary element-wise division, check that the denominator entries are nonzero before interpreting the results as finite physical quantities.
Check sizes and orientation
Equal-sized arrays pair naturally, but MATLAB also supports implicit expansion: an input dimension of size one expands to match the corresponding dimension of the other input. For these element-wise operations, each pair of dimension sizes must be equal, or one must be one.
A scalar therefore works with an array of any size.
Example: x + 10 produces [12 14 16]. Scalar multiplication also scales every entry: 2*x and 2.*x both produce [4 8 12].
Orientation matters. A 1×3 row vector and a 2×1 column vector expand to a 2×3 result. Each output row uses one column-vector entry, and each output column uses one row-vector entry. In contrast, arrays sized 2×3 and 2×2 are incompatible for element-wise arithmetic because their column counts differ and neither is one.
Recognize matrix operations
Matrix multiplication combines entries through row-by-column sums. For C = A*B,
The inner dimensions must match for nonscalar matrix multiplication. The output has the row count of A and column count of B.
For example:
A = [1 2; 3 4];
B = [2 0; 1 3];
C = A*B;
D = A.*B;Here, C = [4 6; 10 12], whereas D = [2 0; 3 12]. Both expressions are valid, but they perform different calculations.
For a square matrix, A^2 means A*A; A.^2 squares individual entries. Matrix division also has a distinct meaning: for a nonsingular square A, A\b solves the matrix equation Ax=b. Similarly, B/A solves XA=B. These operators do not generally calculate corresponding-entry quotients.
Build and check a complete expression
Translate a physical formula one operation at a time. If row vectors F and m contain matched forces in newtons and masses in kilograms, a = F./m gives accelerations in metres per second squared. MATLAB stores the numbers; you must track the units consistently.
Use parentheses to preserve the intended grouping.
Example: z = (x + 1)./y adds one before dividing each matched pair. To total the entries of a vector z, use sum(z).
What to remember
- Use .*, ./, and .^ for corresponding-entry calculations.
- Check dimensions and orientation before relying on implicit expansion.
- Use matrix operators when the mathematics combines entries through linear algebra.
- Verify the grouping, output size, and physical units.
Example: Energy Used in Three Resistor Tests
A laboratory records three independent DC resistor tests. Each entry in the following row vectors belongs to the same test:
Vraw = [6.5 12.5 15.5]; % measured voltage, V
R = [12 16 30]; % resistance, ohms
t = [4 6 8]; % test duration, sThe voltage instrument reads 0.5 V too high in every test. Assume the corrected voltage and resistance remain constant throughout each test. For each individual test, use
Report powers to one decimal place and energies to the nearest joule.
a) Write a MATLAB expression for the corrected voltages and calculate their values.
Subtract the positive instrument bias from every measured voltage. Because the correction is a scalar, MATLAB applies it to every entry without changing the row-vector shape.
V = Vraw - 0.5;The corrected voltages are 6.0 V, 12.0 V, and 15.0 V. Each is lower than its measured value, as required for an instrument that reads too high.
b) Write a MATLAB expression for the three powers and calculate them.
Each corrected voltage must be squared individually and divided by the resistance at the matching position. Both operations therefore use dotted operators.
P = V.^2./R;First square the voltage entries:
Then divide each squared voltage by its matching resistance:
Thus, P contains [3.0 9.0 7.5] W. The undotted expression V^2 would request a matrix square, rather than squaring the three measurements separately.
c) Calculate the energy used in each test and the combined energy used across all three tests.
Power is constant during each test, so multiply each power by its matching duration. After obtaining the three individual energies, sum their entries to calculate the combined energy.
E = P.*t;
Etotal = sum(E);The unit check is W⋅s=J. Therefore,
The individual energies are 12 J, 54 J, and 60 J, and the combined energy is 126 J. The element-wise product preserves the individual tests; the subsequent sum combines them.
A MATLAB script stores two 3-by-3 matrices of sensor data in the variables A and B. Match each expression with a description of what MATLAB computes when it runs that expression.
Squares every entry independently
Multiplies each entry of the first array by the matching entry of the second
Multiplies the square matrix by itself using matrix multiplication
Adds the same scalar to every entry
Matrix product; columns of the first must equal rows of the second
Divides each entry of the first array by the matching entry of the second
A ./ B
A + 5
A .^ 2
A .* B
A ^ 2
A * B
An engineering model uses the dimensionless matrix A. MATLAB evaluates the following statements:
A = [2 1; 0 3];
D = A^2 - A.^2;What is the value of D?