A matrix is a collection of numbers ordered in the manner of rows and columns: this is a highly convenient way to store lots of data. One example of such data may be the scores of several students (rows) on several exams (columns). Usually these numbers are enclosed inside square brackets in a matrix:

## Adding and Subtracting Matrices

Matrix addition and subtraction operate on matrices element-by-element. The two input matrices should be of same dimensions. The result is the new matrix of the same dimensions where each element of new matrix is the sum or difference of each corresponding input element. For example, consider combination of portfolios of different quantities for same stocks

For illustration lets take an example: 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

## Multiplying Matrices

Matrix multiplication does not operate on matrices element-by-element. It operates following the rules of linear algebra. For multiplying matrices user should remember this key rule: the inner dimensions should be the same. That is, if the first matrix is m-by-3, the second should be 3-by-n. The resulting matrix is m-by-n.

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

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

Portfolios = [100 200

500 400

300 150];

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

PortfolioValues = WeekClosePr * Portfolios

## Dividing Matrices

Matrix division is useful mainly for solving equations, and especially for solving simultaneous linear equations . For example, to solve for X in A*X = B.

MATLAB simplifies the process by providing two matrix division symbols, left and right (\ and /). In general,

X = A\B used to solve for X in A*X = B and

X = B/A used to solve for X in X*A = B.