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


Cij=AijBij.C_{ij}=A_{ij}B_{ij}.


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:


MATLAB
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×31\times3 row vector and a 2×12\times1 column vector expand to a 2×32\times3 result. Each output row uses one column-vector entry, and each output column uses one row-vector entry. In contrast, arrays sized 2×32\times3 and 2×22\times2 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,


Cij=∑k=1nAikBkj,(m×n)(n×p)⟶m×p.C_{ij}=\sum_{k=1}^{n}A_{ik}B_{kj}, \qquad (m\times n)(n\times p)\longrightarrow m\times p.


The inner dimensions must match for nonscalar matrix multiplication. The output has the row count of A and column count of B.


For example:


MATLAB
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=bAx=b. Similarly, B/A solves XA=BXA=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:


MATLAB
Vraw = [6.5 12.5 15.5];    % measured voltage, V
R = [12 16 30];            % resistance, ohms
t = [4 6 8];               % test duration, s


The voltage instrument reads 0.5 V0.5\ \text{V} too high in every test. Assume the corrected voltage and resistance remain constant throughout each test. For each individual test, use


P=V2R,E=Pt.P=\frac{V^2}{R},\qquad E=Pt.


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.


MATLAB
V = Vraw - 0.5;


V=[6.5−0.512.5−0.515.5−0.5]=[6.012.015.0] V.V=[6.5-0.5\quad12.5-0.5\quad15.5-0.5] =[6.0\quad12.0\quad15.0]\ \text{V}.


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.


MATLAB
P = V.^2./R;


First square the voltage entries:


[V12V22V32]=[6.0212.0215.02]=[36144225] V2.[V_1^2\quad V_2^2\quad V_3^2]=[6.0^2\quad12.0^2\quad15.0^2] =[36\quad144\quad225]\ \text{V}^2.


Then divide each squared voltage by its matching resistance:


P1=3612=3.0 W,P2=14416=9.0 W,P3=22530=7.5 W.\begin{aligned} P_1&=\frac{36}{12}=3.0\ \text{W},\\ P_2&=\frac{144}{16}=9.0\ \text{W},\\ P_3&=\frac{225}{30}=7.5\ \text{W}. \end{aligned}


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.


MATLAB
E = P.*t;
Etotal = sum(E);


E1=(3.0 W)(4 s)=12 J,E2=(9.0 W)(6 s)=54 J,E3=(7.5 W)(8 s)=60 J.\begin{aligned} E_1&=(3.0\ \text{W})(4\ \text{s})=12\ \text{J},\\ E_2&=(9.0\ \text{W})(6\ \text{s})=54\ \text{J},\\ E_3&=(7.5\ \text{W})(8\ \text{s})=60\ \text{J}. \end{aligned}


The unit check is W⋅s=J\text{W}\cdot\text{s}=\text{J}. Therefore,


Etotal=12+54+60=126 J.E_{\text{total}}=12+54+60=126\ \text{J}.


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.

Three independent DC tests have voltage and current measurements stored in matching row vectors:


MATLAB
V = [12 18 24];       % volts
I = [1.5 2.0 0.5];    % amperes


For each test, electrical power is P=VIP=VI. Which expression returns a row vector containing the power of each test in watts?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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.

Match each term
A.

Squares every entry independently

B.

Multiplies each entry of the first array by the matching entry of the second

C.

Multiplies the square matrix by itself using matrix multiplication

D.

Adds the same scalar to every entry

E.

Matrix product; columns of the first must equal rows of the second

F.

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

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineering model uses the dimensionless matrix A. MATLAB evaluates the following statements:


MATLAB
A = [2 1; 0 3];
D = A^2 - A.^2;


What is the value of D?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice