Main Content

Matrix Algebra Refresher


The explanations in the sections that follow should help refresh your skills for using matrix algebra and using MATLAB® functions.

In addition, Macro-Investment Analysis by William Sharpe also provides an excellent explanation of matrix algebra operations using MATLAB. It is available on the web at:


When you are setting up a problem, it helps to "talk through" the units and dimensions associated with each input and output matrix. In the example under Multiplying Matrices, one input matrix has five days' closing prices for three stocks, the other input matrix has shares of three stocks in two portfolios, and the output matrix therefore has five days' closing values for two portfolios. It also helps to name variables using descriptive terms.

Adding and Subtracting Matrices

Matrix addition and subtraction operate element-by-element. The two input matrices must have the same dimensions. The result is a new matrix of the same dimensions where each element is the sum or difference of each corresponding input element. For example, consider combining portfolios of different quantities of the same stocks (“shares of stocks A, B, and C [the rows] in portfolios P and Q [the columns] plus shares of A, B, and C in portfolios R and S”).

Portfolios_PQ = [100   200
                 500   400
                 300   150];

Portfolios_RS = [175   125
                 200   200
                 100   500];

NewPortfolios = Portfolios_PQ + Portfolios_RS
NewPortfolios =

        275           325
        700           600
        400           650

Adding or subtracting a scalar and a matrix is allowed and also operates element-by-element.

SmallerPortf = NewPortfolios-10
SmallerPortf =
        265.00        315.00
        690.00        590.00
        390.00        640.00

Multiplying Matrices

Matrix multiplication does not operate element-by-element. It operates according to the rules of linear algebra. In multiplying matrices, it helps to remember this key rule: the inner dimensions must be the same. That is, if the first matrix is m-by-3, the second must be 3-by-n. The resulting matrix is m-by-n. It also helps to “talk through” the units of each matrix, as mentioned in Analyze Sets of Numbers Using Matrix Functions.

Matrix multiplication also is not commutative; that is, it is not independent of order. A*B does not equal B*A. The dimension rule illustrates this property. If A is 1-by-3 matrix and B is 3-by-1 matrix, A*B yields a scalar (1-by-1) matrix but B*A yields a 3-by-3 matrix.

Multiplying Vectors

Vector multiplication follows the same rules and helps illustrate the principles. For example, a stock portfolio has three different stocks and their closing prices today are:

ClosePrices = [42.5   15   78.875]

The portfolio contains these numbers of shares of each stock.

NumShares = [100

To find the value of the portfolio, multiply the vectors

PortfValue = ClosePrices * NumShares

which yields:

PortfValue =


The vectors are 1-by-3 and 3-by-1; the resulting vector is 1-by-1, a scalar. Multiplying these vectors thus means multiplying each closing price by its respective number of shares and summing the result.

To illustrate order dependence, switch the order of the vectors

Values = NumShares * ClosePrices
Values =

  1.0e+004 *

    0.4250    0.1500    0.7887
    2.1250    0.7500    3.9438
    1.2750    0.4500    2.3663

which shows the closing values of 100, 500, and 300 shares of each stock, not the portfolio value, and this is meaningless for this example.

Computing Dot Products of Vectors

In matrix algebra, if X and Y are vectors of the same length


then the dot product


is the scalar product of the two vectors. It is an exception to the commutative rule. To compute the dot product in MATLAB, use sum(X .* Y) or sum(Y .* X). Be sure that the two vectors have the same dimensions. To illustrate, use the previous vectors.

Value = sum(NumShares .* ClosePrices')
Value =

Value = sum(ClosePrices .* NumShares')
Value =


As expected, the value in these cases matches the PortfValue computed previously.

Multiplying Vectors and Matrices

Multiplying vectors and matrices follows the matrix multiplication rules and process. For example, a portfolio matrix contains closing prices for a week. A second matrix (vector) contains the stock quantities in the portfolio.

WeekClosePr = [42.5     15      78.875
               42.125   15.5    78.75
               42.125   15.125  79
               42.625   15.25   78.875
               43       15.25   78.625];
PortQuan = [100

To see the closing portfolio value for each day, simply multiply

WeekPortValue = WeekClosePr * PortQuan
WeekPortValue =

1.0e+004 *


The prices matrix is 5-by-3, the quantity matrix (vector) is 3-by-1, so the resulting matrix (vector) is 5-by-1.

Multiplying Two Matrices

Matrix multiplication also follows the rules of matrix algebra. In matrix algebra notation, if A is an m-by-n matrix and B is an n-by-p matrix

A=[a11a12a1nai1ai2ainam1am2amn],  B=[b11b1jb1pb21b2jb2pbn1bnjbnp]

then C = A*B is an m-by-p matrix; and the element cij in the ith row and jth column of C is


To illustrate, assume that there are two portfolios of the same three stocks previously mentioned but with different quantities.

Portfolios = [100   200
              500   400
              300   150];

Multiplying the 5-by-3 week's closing prices matrix by the 3-by-2 portfolios matrix yields a 5-by-2 matrix showing each day's closing value for both portfolios.

PortfolioValues = WeekClosePr * Portfolios
PortfolioValues =

1.0e+004 *

    3.5412    2.6331
    3.5587    2.6437
    3.5475    2.6325
    3.5550    2.6456
    3.5513    2.6494

Monday's values result from multiplying each Monday closing price by its respective number of shares and summing the result for the first portfolio, then doing the same for the second portfolio. Tuesday's values result from multiplying each Tuesday closing price by its respective number of shares and summing the result for the first portfolio, then doing the same for the second portfolio. And so on, through the rest of the week. With one simple command, MATLAB quickly performs many calculations.

Multiplying a Matrix by a Scalar

Multiplying a matrix by a scalar is an exception to the dimension and commutative rules. It just operates element-by-element.

Portfolios = [100   200
              500   400
              300   150];

DoublePort = Portfolios * 2
DoublePort =
        200           400
       1000           800
        600           300

Dividing Matrices

Matrix division is useful primarily for solving equations, and especially for solving simultaneous linear equations (see Solving Simultaneous Linear Equations). For example, you want to solve for X in A*X = B.

In ordinary algebra, you would divide both sides of the equation by A, and X would equal B/A. However, since matrix algebra is not commutative (A*XX*A), different processes apply. In formal matrix algebra, the solution involves matrix inversion. MATLAB, however, simplifies the process by providing two matrix division symbols, left and right (\ and /). In general,

X = A\B solves for X in A*X = B and

X = B/A solves for X in X*A = B.

In general, matrix A must be a nonsingular square matrix; that is, it must be invertible and it must have the same number of rows and columns. (Generally, a matrix is invertible if the matrix times its inverse equals the identity matrix. To understand the theory and proofs, consult a textbook on linear algebra such as Elementary Linear Algebra by Hill listed in Bibliography.) MATLAB gives a warning message if the matrix is singular or nearly so.

Solving Simultaneous Linear Equations

Matrix division is especially useful in solving simultaneous linear equations. Consider this problem: Given two portfolios of mortgage-based instruments, each with certain yields depending on the prime rate, how do you weight the portfolios to achieve certain annual cash flows? The answer involves solving two linear equations.

A linear equation is any equation of the form


where a1, a2, and b are constants (with a1 and a2 not both 0), and x and y are variables. (It is a linear equation because it describes a line in the xy-plane. For example, the equation 2x + y = 8 describes a line such that if x = 2, then y = 4.)

A system of linear equations is a set of linear equations that you usually want to solve at the same time; that is, simultaneously. A basic principle for exact answers in solving simultaneous linear equations requires that there be as many equations as there are unknowns. To get exact answers for x and y, there must be two equations. For example, to solve for x and y in the system of linear equations


there must be two equations, which there are. Matrix algebra represents this system as an equation involving three matrices: A for the left-side constants, X for the variables, and B for the right-side constants

A=[2113],     X=[xy],     B=[1318],

where A*X = B.

Solving the system simultaneously means solving for X. Using MATLAB,

A = [2  1
     1 -3];

B = [13
X = A \ B

solves for X in A * X = B.

X = [3 7]

So x = 3 and y = 7 in this example. In general, you can use matrix algebra to solve any system of linear equations such as


by representing them as matrices

A=[a11a12a1na21a22a2nam1am2amn],     X=[x1x2xn],     B=[b1b2bm]

and solving for X in A*X = B.

To illustrate, consider this situation. There are two portfolios of mortgage-based instruments, M1 and M2. They have current annual cash payments of $100 and $70 per unit, respectively, based on today's prime rate. If the prime rate moves down one percentage point, their payments would be $80 and $40. An investor holds 10 units of M1 and 20 units of M2. The investor's receipts equal cash payments times units, or R = C * U, for each prime-rate scenario. As word equations:




Prime flat:

$100 * 10 units

+ $70 * 20 units = $2400 receipts

Prime down:

$80 * 10 units

+ $40 * 20 units = $1600 receipts

As MATLAB matrices:

Cash = [100  70
         80  40];

Units = [10

Receipts = Cash * Units
Receipts =


Now the investor asks this question: Given these two portfolios and their characteristics, how many units of each should they hold to receive $7000 if the prime rate stays flat and $5000 if the prime drops one percentage point? Find the answer by solving two linear equations.




Prime flat:

$100 * x units

+ $70 * y units = $7000 receipts

Prime down:

$80 * x units

+ $40 * y units = $5000 receipts

In other words, solve for U (units) in the equation R (receipts) = C (cash) * U (units). Using MATLAB left division

Cash = [100  70
         80  40];

Receipts = [7000

Units = Cash \ Receipts
Units =


The investor should hold 43.75 units of portfolio M1 and 37.5 units of portfolio M2 to achieve the annual receipts desired.

Operating Element by Element

Finally, element-by-element arithmetic operations are called operations. To indicate a MATLAB array operation, precede the operator with a period (.). Addition and subtraction, and matrix multiplication and division by a scalar, are already array operations so no period is necessary. When using array operations on two matrices, the dimensions of the matrices must be the same. For example, given vectors of stock dividends and closing prices

Dividends = [1.90  0.40  1.56  4.50];
Prices = [25.625  17.75  26.125  60.50];

Yields = Dividends ./ Prices
Yields =

    0.0741    0.0225    0.0597    0.0744

Related Topics