introduction to matlab vector and matrix

Post on 03-Jun-2018

236 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 1/47

Introduction to MatlabVector and Matrix Operation

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 2/47

Matlab basic operations

• MATLAB is based on matrix/vector mathematics

• Entering matrices

• Enter an explicit list of elements

• Load matrices from external data files

• Generate matrices using built-in functions

• Create vectors with the colon (:) operator

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 3/47

Entering a Matrix in MATLAB

2 -3 5-1 4 6

 

A

• MATLAB Format:

>> A = [2, -3, 5; -1, 4, 5]

A =

2 -3 5

-1 4 5

3

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 4/47

Entering a Row Vector in MATLAB

[1 4 7]x

• MATLAB Format:

>> x = [1, 4, 7]

x =

1 4 7

4

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 5/47

Entering a Column Vector in MATLAB

1

4

7

x

• MATLAB Format

>> x = [1; 4; 7]

x =

1

4

7

5

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 6/47

Generate matrices using built-infunctions

• Functions such as zeros(), ones(), eye(), magic(), etc.

>> A=zeros(3)

 A =

0 0 0

0 0 0

0 0 0

>> B=ones(3,2)

B =

1 1

1 1

1 1 

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 7/47

>> I=eye(4) (i.e., identity matrix)

I =

1 0 0 0

0 1 0 0

0 0 1 00 0 0 1

>> A=magic(4) (i.e., magic square)

A =

16 2 3 13

5 11 10 8

9 7 6 124 14 15 1

>>

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 8/47

Generate Vectors with Colon (:) Operator

The colon operator uses the following rules to create regularly spaced

vectors:

 j:k is the same as [j,j+1,...,k]

 j:k is empty if j > k

 j:i:k is the same as [j,j+i,j+2i, ...,k]

 j:i:k is empty if i > 0 and j > k or if i < 0 and j < k

where i, j, and k are all scalars.

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 9/47

>> c=0:5

c =

0 1 2 3 4 5

>> b=0:0.2:1

b =0 0.2000 0.4000 0.6000 0.8000 1.0000

>> d=8:-1:3

d =

8 7 6 5 4 3

>> e=8:2

e =

Empty matrix: 1-by-0 

Examples

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 10/47

Extracting a Sub-Matrix

• A portion of a matrix can be extracted and stored in a smaller matrixby specifying the names of both matrices, the rows and columns. Thesyntax is:

sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;

where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make thenew matrix.

10

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 11/47

MATLAB Matrices

• A column vector can beextracted from a matrix. Asan example we create amatrix below:

» matrix=[1,2,3;4,5,6;7,8,9]

matrix =

1 2 3

4 5 67 8 9

• Here we extract column 2 ofthe matrix and make acolumn vector:

» col_two=matrix( : , 2)

col_two =

2

5

8

11

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 12/47

MATLAB Matrices

• A row vector can be extractedfrom a matrix. As an examplewe create a matrix below:

» matrix=[1,2,3;4,5,6;7,8,9]

matrix =

1 2 3

4 5 67 8 9

• Here we extract row 2 of thematrix and make a row vector.Note that the 2:2 specifies thesecond row and the 1:3specifies which columns of therow.

» rowvec=matrix(2 : 2 , 1 : 3)

rowvec =

4 5 6

12

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 13/47

Matrices transpose

• a vector x = [1, 2, 5, 1]

x =

1 2 5 1

• transpose y = x’ y =

1

2

5

1

13

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 14/47

Basic Permutation of Matrix in MATLAB

• sum, transpose, and diag

• Summation

We can use sum() function.

Examples, 

>> X=ones(1,5)

X =

1 1 1 1 1

>> sum(X)

ans =

5

>>

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 15/47

>> A=magic(4)

 A =

16 2 3 13

5 11 10 8

9 7 6 124 14 15 1

>> sum(A)

ans =

34 34 34 34

>>

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 16/47

Transpose

>> A=magic(4)

 A =

16 2 3 13

5 11 10 8

9 7 6 124 14 15 1

>> A'

ans =

16 5 9 42 11 7 14

3 10 6 15

13 8 12 1

>>

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 17/47

Scalar - Matrix Addition

» a=3;

» b=[1, 2, 3;4, 5, 6]

b =

1 2 3

4 5 6

» c= b+a % Add a to each element of b

c =

4 5 6

7 8 9

17

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 18/47

Scalar - Matrix Subtraction

» a=3;

» b=[1, 2, 3;4, 5, 6]

b =

1 2 34 5 6

» c = b - a %Subtract a from each element of b

c =

-2 -1 01 2 3

18

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 19/47

Scalar - Matrix Multiplication

» a=3;

» b=[1, 2, 3; 4, 5, 6]

b =1 2 3

4 5 6

» c = a * b % Multiply each element of b by a

c =

3 6 9

12 15 18

19

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 20/47

Scalar - Matrix Division

» a=3;

» b=[1, 2, 3; 4, 5, 6]

b =1 2 3

4 5 6

» c = b / a % Divide each element of b by a

c =

0.3333 0.6667 1.0000

1.3333 1.6667 2.0000

20

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 21/47

Matrix Addition and Subtraction

•Matrix addition and subtraction with MATLAB are achieved in thesame manner as with scalars provided that the matrices have thesame size. Typical expressions are shown below.

>> C = A + B

>> D = A - B

21

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 22/47

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 23/47

23

Matrix Multiplication

Matrix multiplication with MATLAB isachieved in the same manner as withscalars provided that the number of

columns of the first matrix is equal to thenumber of rows of the second matrix. Atypical expression is

>> E = A*B

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 24/47

Matrix Multiplication

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 25/47

 Array Multiplication

•There is another form of multiplication of matrices in which it isdesired to multiply corresponding elements in a fashion similar tothat of addition and subtraction. This operation arises frequently withMATLAB, and we will hereafter refer to the process as the arrayproduct to distinguish it from the standard matrix multiplication form.

25

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 26/47

 Array Multiplication Continuation

•For the array product to be possible, the two matrices must have thesame size, as was the case for addition and subtraction. The resultingarray product will have the same size. If F represents the resultingmatrix, a given element of F, denoted by f ij   is determined by thecorresponding product from the two matrices as

ij ij ij f a b26

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 27/47

MATLAB Array Multiplication

•To form an array product in MATLAB, a period must be placed afterthe first variable. The operation is commutative. The following twooperations produce the same result.

>> F=A.*B

>> F=B.*A

27

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 28/47

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 29/47

29

MATLAB Array MultiplicationContinuation

If there are more than two matrices forwhich array multiplication is desired, the

periods should follow all but the last onein the expression; e. g., A.*B.*C in thecase of three matrices. Alternately,nesting can be used; e.g. (A.*B).*C for

the case of three matrices.

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 30/47

MATLAB Array MultiplicationContinuation

•The array multiplication concept arises in any operation in which thecommand could be “confused” for a standard matrix operation. Forexample, suppose it is desired to form a matrix B from a matrix A byraising each element of A to the 3rd power, The MATLAB command is

>> B = A.^3

30

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 31/47

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 32/47

Determinant of a Matrix

•The determinant of a square matrix in MATLAB is determined by thesimple command det(A). Thus, if a is to represent the determinant,we would type and enter

>> a = det(A)

•Note that a is a scalar (1 x 1 "matrix").

32

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 33/47

Inverse Matrix

•The inverse of a square matrix in MATLAB is determined by the simplecommand inv(A). Thus, if B is to represent the inverse of A , thecommand would be

>> B = inv(A)

33

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 34/47

Simultaneous Equation Solution

Ax = b

•MATLAB Format:

>> x = inv(A)*b

•Alternate MATLAB Format:

>> x = A\b

34

-1x = A b

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 35/47

Example 3-1. Enter the matrices belowin MATLAB. They will be used in the

next several examples.

2 -3 5

-1 4 6

 

A

>> A = [2 -3 5; -1 4 6];

>> B = [2 1; 7 -4; 3 1];

35

2 1

7 -4

3 1

B

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 36/47

Example 3-2. Determine the transposeof B and denote it as C.

>> C = B'

C =

2 7 3

1 -4 1

•The 3 x 2 matrix has been converted to a 2 x 3 matrix.

36

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 37/47

Example 3-3. Determine the sum of Aand C and denote it as D.

>> D = A + C

D =

4 4 8

0 0 7

37

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 38/47

Example 3-4. Determine the product of Aand B with A first.

>> A*B

ans =

-2 19

44 -11

38

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 39/47

Example 3-5. Determine the product of Band A with B first.

>> B*A

ans =

3 -2 16

18 -37 11

5 -5 21

39

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 40/47

Example 3-6. Determine the array productof A and C and denote it as E.

>> E = A.*C

E =

4 -21 15

-1 -16 6

40

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 41/47

Example 3-7. Enter the matrix A. It will be usedin several examples.

1 2 -1

-1 1 3

 3 2 1

A

>> A = [1 2 -1; -1 1 3; 3 2 1]

A =

1 2 -1

-1 1 3

3 2 1

41

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 42/47

Example 3-7. Continuation. Determine thedeterminant of A and denote it as a.

>> a = det(A)

a =

20

42

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 43/47

Example 3-8. Determine the inverse matrixof A and denote it as Ainv.

>> Ainv = inv(A)

Ainv =

-0.2500 -0.2000 0.3500

0.5000 0.2000 -0.1000

-0.2500 0.2000 0.1500

43

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 44/47

44

Example 3-9. Use MATLAB to solvethe simultaneous equations below.

1 2 32 8 x x x

1 2 33 7 x x x

1 2 33 2 4 x x x

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 45/47

Example 3-9. Continuation.

• Assume that A is still in memory.

>> b = [-8; 7; 4]

b =

-8

7

4

45

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 46/47

Example 3-9. Continuation.

>> x = inv(A)*b

x =

2.0000

-3.0000

4.0000

46

8/12/2019 Introduction to Matlab Vector and Matrix

http://slidepdf.com/reader/full/introduction-to-matlab-vector-and-matrix 47/47

Example 3-9. Continuation.• Alternately,

>> x = A\b

x =

2.0000

-3.0000

4.0000

top related