• Tidak ada hasil yang ditemukan

Lab 2

N/A
N/A
Protected

Academic year: 2025

Membagikan "Lab 2"

Copied!
29
0
0

Teks penuh

(1)

LAB 2

Vectors and Matrices Dr.Abdel Fattah FARES

(2)
(3)

Vectors :

A vector is a one-dimensional array of numbers. MATLAB allows you to create column vectors or row vectors.

Column vectors

>> a = [2; 1; 4]

a = 2 1 4

row vectors

>> v = [2 0 4]

v =

2 0 4

>> w = [1,1,9]

w =

1 1 9

(4)

>> A = [1 2 3; 5 7 11; 13 17 19]

A =

1 2 3

5 7 11

13 17 19

(5)

Transpose operation

In MATLAB, we represent the transpose operation with a

single quote or tick mark (‘). Taking the transpose of a column vector produces a row vector

:

(6)

>> v = [2 4 1 7]

v =

2 4 1 7

>> w = v’

w = 2 4 1 7

Transpose Operator (1)

(7)

Transpose Operator (2)

(8)

Add or Subtract two vectors

Vectors with Uniformly Spaced Elements x = [xi : q : xe]

(9)

The length command returns the number of elements that a vector contains. For

example:

The length :

MAX & MIN

(10)

>> x=[0:3:10]

x =

0 3 6 9

(11)

array multiplication

summation

square root

(12)
(13)
(14)
(15)
(16)

Matrices :

(17)

Matrices scalar multiplication

add and subtract

(18)

The transpose

Array multiplication

(19)

Matrix Multiplication

Consider two matrices A and B. If A is an m × p matrix and B is a p × n matrix, they can be multiplied together to produce an m × n matrix. To do this in MATLAB, we leave out the period (.) and simply write A*B. Keep in mind that if the dimensions of the two matrices are not correct, the operation will generate an error.

(20)
(21)

More Basic Operations

(22)

Creating vectors with linspace (1)

The linspace function creates vectors with elements having uniform

linear spacing.

Syntax:

x = linspace(startValue,endValue,nelements) Examples:

>> u = linspace(0.0,0.25,5) u =

0 0.0625 0.1250 0.1875 0.2500

(23)

Creating vectors with linspace (2)

Column vectors are created by appending the transpose operator to

linspace

>> v = linspace(0,9,4)’

v = 0 3 6 9

(24)

Special Matrix Types

The identity matrix is a square matrix that has ones along the diagonal and zeros elsewhere. To create an n × n identity matrix, type the following MATLAB command:

eye(n) Syntax :

A = eye(n)

A = eye(nrows,ncols)

Let’s create a 4 × 4 identity matrix:

(25)

Use ones and zeros to set intial values of a matrix or vector.

Syntax:

A = ones(nrows,ncols) A = zeros(nrows,ncols)

(26)

The diag function can either create a matrix with specified diagonal

elements, or extract the diagonal elements from a matrix

(27)
(28)

Referencing Matrix Elements

(29)

change the value of matrix elements delete a row or column in a matrix

Referensi

Dokumen terkait