02.03.2016 Views

MATLAB by rudra pratap

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.2 Matrix and Array Operations<br />

73<br />

3.2 Matrix and Array Operations<br />

3.2.1 Arithmetic operations<br />

For people who are used to programming in a conventional language such as Pascal,<br />

Fortran, or C, it is an absolute delight to be able to write a matrix product as C=A*B<br />

where A is an m x n matrix and B is an n x k matrix. <strong>MATLAB</strong> allows<br />

+<br />

*<br />

I<br />

' (caret)<br />

addition<br />

subtraction<br />

multiplication<br />

division<br />

exponentiation<br />

to be carried out on matrices in straightforward ways as long as the operation makes<br />

sense mathematically and the operands are compatible. Thus,<br />

A+B or A-B<br />

A*B<br />

A/B<br />

is valid if A and B are of the same size,<br />

is valid if A's number of columns equals B's number of rows,<br />

is valid and equals A · B- 1 for same-size square matrices, and<br />

which equals A*A, makes sense only if A is square.<br />

help arith<br />

In all these commands, if B is replaced <strong>by</strong> a scalar, say a, the arithmetic operations<br />

are still carried out. In this case, the command A+a adds a to each element<br />

of A, the command Ma (or a*A) multiplies each element of A <strong>by</strong> a and so on.<br />

Vectors are just treated as a single row or a column matrix and therefore a command<br />

such as w=u*v, where u and v are same-size vectors, say m x 1, produces an<br />

error (because you cannot multiply an m x 1 matrix with an m x 1 matrix), whereas<br />

w=u*v' and w=u' *V execute correctly, producing the outer and inner products of the<br />

two vectors, respectively (see examples in Fig 2.7 on page 41).<br />

The left division: In addition to the normal or right division (/), there is a<br />

left division (\) in <strong>MATLAB</strong>. This division is used to solve a matrix equation. In<br />

particular, the command x=A \ b solves the matrix equation Ax = b. Thus A\ b is<br />

almost the same as inv (A) *b but faster and more numerically stable than computing<br />

inv (A) *b. In the deg

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!