02.03.2016 Views

MATLAB by rudra pratap

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.1 Matrices and Vectors<br />

65<br />

3.1 Matrices and Vectors<br />

3.1.1 Input<br />

A matrix is entered row-wise, with consecutive elements of a row separated <strong>by</strong> a<br />

space or a comma, and the rows separated <strong>by</strong> semicolons or carriage returns. The<br />

entire matrix must be enclosed within square brackets . Elements of the matrix may<br />

be real numbers, complex numbers, or valid <strong>MATLAB</strong> expressions. 2<br />

Examples:<br />

Matrix<br />

A=[<br />

B =<br />

[ 2x<br />

5i<br />

2<br />

9 ]<br />

ln x + sin y<br />

3 + 2i<br />

Special cases: Vectors and scalars<br />

<strong>MATLAB</strong> input command<br />

A = [1 2 5; 3 9 0]<br />

B [2*x log (x)+sin(y) ; 5i 3+2i]<br />

(see the note in the margin.)<br />

• A vector is a special case of a matrix, with just one row or one column. It is<br />

entered the same way as a matrix.<br />

Examples: u = [1 3 9] produces a row vector, and<br />

v = [1; 3; 9] produces a column vector.<br />

• A scalar does not need brackets.<br />

Example: g = 9. 81;<br />

• Square brackets with no elements between them create a null matrix.<br />

Example: X = []. (See Fig. 3.1 for a more useful example) .<br />

Fo r on-line help' ·<br />

•tv:P8:,:<br />

hep<br />

• "· ""<br />

For the matrix<br />

B command to<br />

work, variables<br />

x and y must be<br />

predefined, e.g.,<br />

x=l ; y=2 ;, etc.<br />

Continuation<br />

If it is not possible to type the entire input on the same line, then use three consecutive<br />

periods ( .. . ) to signal continuation and continue the input on the next line.<br />

The three periods are called an ellipsis. For example,<br />

A = [1/3 5.55*sin (x) 9.35 0.097 ; . . .<br />

3/ (x+2*log(x) ) 3 0 6.555 ; .. .<br />

(5*x-23)/55 x-3 x*sin(x) sqrt(3)] ;<br />

produces the intended 3 x 4 matrix A (provided, of course, x has been assigned<br />

a value before) . A matrix can also be entered across multiple lines using carriage<br />

returns at the end of each row. In this case, the semicolons and ellipses at the end<br />

of each row may be omitted. Thus, the following three commands are equivalent:<br />

A = [1 3 9; 5 10 15; 0 o -5] ;<br />

A = [1 3 9<br />

5 10 15<br />

0 0 -5] ;<br />

A [1 3 9; 5 10 ...<br />

15 ; 0 0 -5] ;<br />

2 The box on the right lets you know that you can learn more about this topic from M.A'T'LAB's<br />

on-line help, on the help topic called elmat (for elementary matrix manipulations) .

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

Saved successfully!

Ooh no, something went wrong!