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.

66 Interactive Computation<br />

Continuation across several input lines achieved through an ellipsis is not limited<br />

to matrix input. This construct may be used for other commands and for a long<br />

list of command arguments (see Section 4.3.2, page 112), as long as the command<br />

does not exceed 4,096 characters.<br />

3.1.2 Indexing (or subscripting)<br />

Once a matrix exists, its elements are accessed <strong>by</strong> specifying their row and column<br />

indices. Thus, A ( i , j) in <strong>MATLAB</strong> refers to the element aij of matrix A, i.e.,<br />

the clement in the ith row and jth column. This notation is fairly common in<br />

computational software packages and programming languages. <strong>MATLAB</strong>, however,<br />

provides a much higher level of index specification-it allows a range of rows and<br />

columns to be specified at the same time. For example, the statement A(m:n,k:l)<br />

specifies rows rn to n and columns k to l of matrix A. When the rows (or columns)<br />

to be specified range over all rows (or columns) of the matrix, a colon can be used<br />

as the row (or column) index. Thus, A(: ,5:20) refers to the elements in columns<br />

5 through 20 of all the rows of matrix A. This feature makes matrix manipulation<br />

much easier and provides a way to take advantage of the "vectorized" nature of<br />

calculations in <strong>MATLAB</strong>. (See Fig. 3.1 for examples.)<br />

Dimension<br />

Matrix dimensions are determined automatically <strong>by</strong> <strong>MATLAB</strong>, i.e., no explicit dimension<br />

declarations are required. The dimensions of an existing matrix A may be<br />

obtained with the command size (A) or more explicitly with [m , n] =size (A) , which<br />

assigns the number of rows and columns of A to the variables m and n, respectively.<br />

When a matrix is entered <strong>by</strong> specifying a single element or a few elements of the<br />

matrix, <strong>MATLAB</strong> creates a matrix just big enough to accommodate the elements.<br />

Thus, if the matrices B and C do not exist already, then<br />

B(2,3) = 5;<br />

produces<br />

0<br />

0<br />

C(3,1:3) [1 2 3] ; produces<br />

0<br />

0<br />

2<br />

3.1.3 Matrix manipulation<br />

As you can see from examples in Fig. 3.1, it is fairly easy to correct wrong Pntries<br />

of a matrix, extract any part or submatrix of a matrix, or delete or add rows and<br />

columns. These manipulations are done with <strong>MATLAB</strong>'s smart indexing feature.<br />

By specifying vectors as the row and column indices of a matrix, one can reference<br />

and modify any submatrix of a matrix. Thus, if A is a 10 x 10 matrix, B is a 5 X 10<br />

matrix, and y is a 20 elements long row vector, then<br />

A([1 3 6 9] ,:) = [B ( 1:3,:); y(1: 10)]

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

Saved successfully!

Ooh no, something went wrong!