0 views

Uploaded by lennevey

Matlab

- Matrix 1
- Lecture 3 -Manipulating 2D Objects.pptx
- Matlab Tutorial
- rprogramming-sample.pdf
- 3. 2018.02.21 Lesson 3 & 4.txt
- Properties of Matrix Operations
- Lecture 1
- Modeling Transformations.
- Illia Civ Quarterl 395 Univ
- Matlab Practice
- Chap3_Vectors in MATLAB
- Algebra Notes
- 2d arrays
- 3 Arrays
- matlab1
- Eamcet Qr Maths Sr Maths Iia-3. Matrices
- Optimal State-Feedback Control Under Sparsity and Delay Constraints
- pp review
- Discrete Image Processing
- Www Sanfoundry Com

You are on page 1of 71

and Structure

Arrays

Khairul anwar mohamad said

mskanwar@feng.unimas.my

1

Specification of a position vector using Cartesian

coordinates.

specified by three

components: x, y, and

z, and can be written

as:

p = [x, y, z].

However, MATLAB can

use vectors having

more than three

elements.

2

To create a row vector, separate the elements by

semicolons. For example,

>>p = [3,7,9]

p =

3 7 9

transpose notation (').

>>p = [3,7,9]'

p =

3

7

9

3

You can also create a column vector by separating the

elements by semicolons. For example,

>>g = [3;7;9]

g =

3

7

9

4

You can create vectors by ''appending'' one vector to

another.

columns contain the values of r = [2,4,20] and

whose fourth, fifth, and sixth columns contain the values

of w = [9,-6,3], you type u = [r,w]. The result is

the vector ?

u = [2,4,20,9,-6,3].

5

The colon operator (:) easily generates a large vector of

regularly spaced elements.

Typing

>>x = [m:q:n]

value is m. The last value is n if m - n is an integer

multiple of q. If not, the last value is less than n.

6

For example, typing x = [0:2:8] creates the

vector x = [0,2,4,6,8], whereas typing x =

[0:2:7] creates the vector x = [0,2,4,6].

from 5 to 8 in steps of 0.1, type z = [5:0.1:8].

presumed to be 1. Thus typing y = [-3:2]

produces the vector y = [-3,-2,-1,0,1,2].

7

The linspace command also creates a linearly

spaced row vector, but instead you specify the number

of values rather than the increment.

are the lower and upper limits and n is the number of

points.

[5:0.1:8].

8

The logspace command creates an array of

logarithmically spaced elements.

the number of points between 10a and 10b.

produces the vector x = [0.1000,

0.4642, 2.1544, 10.000].

50.

9

Magnitude, Length, and Absolute Value of a Vector

using MATLAB.

the vector.

xn is a scalar, given by x12 + x22 + … + xn2), and is the

same as the vector's geometric length.

elements are the absolute values of the elements of x.

10

For example, if x = [2,-4,5],

(computed from sqrt(x’*x))

from abs(x)).

answer ok

11

Matrices

A matrix has multiple rows and columns. For

example, the matrix

2 4 10

M = 16 3 7

8 4 9

3 12 15

Vectors are special cases of matrices having

one row or one column.

2-12 12

Creating Matrices

the elements in a given row with spaces or commas and

separating the rows with semicolons. For example, typing

>>A = [2,4,10;16,3,7];

2 4 10

A=

16 3 7

different columns, whereas semicolons separate elements

in different rows.

13

Creating Matrices from Vectors

vectors). Note the difference between the results given

by [a b] and [a;b] in the following session:

>>c = [a b];

c =

1 3 5 7 9 11

>>D = [a;b]

D =

1 3 5

7 9 11

14

User can also type in directly the vector to create

matrix

For example, you can type

>> D = [[1,3,5];[7,9,11]];

15

Matrices and the transpose

operation

• Transpose operation will interchange the rows and

columns. The operation symbol is ‘

• Example

• A=[-2,-3; 6,5]

• Tranpose A is

• A’=?

16

Array Addressing

The colon operator selects individual elements, rows, columns, or

''subarrays'' of arrays. Here are some examples:

• v(2:5) represents the second through fifth elements; that is v(2),

v(3), v(4), v(5).

• A(:,3) denotes all the elements in the third column of the matrix

A.

• A(:,2:5) denotes all the elements in the second through fifth

columns of A.

• A(2:3,1:3) denotes all the elements in the second and third

rows that are also in the first through third columns.

17

Example

• Write the following matrices

• b=

3 4 5 3 4

6 7 8 6 7

3 4 5 3 4

Find the b(:), b(2:5), b(:,3), A(:,2:5), A(2:3,1:3)

18

You can use array indices to extract a smaller array from

another array. For example, if you first create the array B

2 4 10 13

B = 16 3 7 18

8 4 9 25

3 12 15 17

then type C = B(2:3,1:3), you can produce the

following array:

16 3 7

C=

8 4 9

19

Refer to exercise note 2

20

Additional Array Functions

find(A) containing the row and column

indices of the nonzero elements

of the matrix A, and the array w,

containing the values of the

nonzero elements. The array w

may be omitted.

elements of A if A is a vector or

length(A) the largest value of m or n if A is

an m × n matrix.

21

Example

6 0 3

B=

0 4 0

2 7 0

Find the non zero element and state their index using command

[u,v,w] = find(A)

22

Answer

23

Additional Array Functions

max(A) Returns the algebraically

largest element in A if A is

a vector.

Returns a row vector

containing the largest

elements in each column if

A is a matrix.

If any of the elements are

complex, max(A) returns

the elements that have the

largest magnitudes.

24

Additional Array Functions

stores the maximum

values in the row vector x

and their indices in the row

vector k.

min(A)

Like max but returns

and minimum values.

[x,k] = min(A)

2-20 25

Additional Array Functions

containing the sizes of the

m x n array A.

array A in ascending order

and returns an array the

same size as A.

column of the array A and

returns a row vector

containing the sums.

26

The function size(A) returns a row vector [m n]

containing the sizes of the m × n array A. The

length(A) function computes either the number of

elements of A if A is a vector or the largest value of m or

n if A is an m × n matrix.

For example, if

6 2

A = –10 –5

3 0

the vector [-10, -5]; size(A) returns [3, 2]; and

length(A) returns 3.

2-22 27

Multidimensional Arrays

third dimension. Each “layer” is called a page.

concatenating the arrays A,B,C,

and so on along the dimension

n.

2-25 28

Example

• A=[8,2; 9,5]

• B=[4,6; 7,3]

• Find three dimensional array using command

cat(n,a,b,....).

>> c=cat(3,A,B)

c(:,:,1) =

8 2

9 5

c(:,:,2) =

4 6

7 3

29

Try this

• Create a matrix Z (3D array) consisting of a 3x3

matrix F, 3x3 matrix G and 3x3 matrix H. Define

matrix F, G and H with your choice of value.

30

Vector addition by geometry. (a) The parallelogram law. (b)

Addition of vectors in three dimensions.

2-26 31

Array Addition and Subtraction

For example:

6 –2 + 9 8 = 15 6 (Eqn. 2.3-1)

10 3 –12 14 –2 17

The addition shown in equation 2.3–1 is performed in

MATLAB as follows:

>>A = [6,-2;10,3];

>>B = [9,8;-12,14]

>>A+B

ans =

15 6

-2 17

2-27 32

Geometric interpretation of scalar multiplication of a vector.

Figure 2.3–1

If r = [x, y, z],

then v = 2r

=2[x, y, z] =

[2x, 2y, 2z].

2-28 33

Multiplying a matrix A by a scalar w produces a matrix

whose elements are the elements of A multiplied by w.

For example:

2 9 6 27

3 5 =

–7 15 –21

>>3*A

ans =

6 27

15 -21

2-29 34

Multiplication of an array by a scalar is easily defined

and easily carried out.

straightforward.

multiplication), and

2-30 35

Division and exponentiation must also be

carefully defined when you are dealing

with operations between two arrays.

operations on arrays. Next we introduce

one form, called array operations, which

are also called element-by-element

operations. Then we will introduce matrix

operations. Each form has its own

applications.

Division and exponentiation must also be

carefully defined when you are dealing

with operations between two arrays.

2-31 36

Element-by-element operations

2.^[3,5]=[2^3,2^5]

[3,5].^[2,4]=[3^2,5^4]

2-32 37

Refer to Questions

38

Array or Element-by-element multiplication is defined

only for arrays having the same size. The definition of

the product x.*y, where x and y each have n

elements, is

x(n)y(n)]

x = [2, 4, – 5], y = [– 7, 3, – 8]

then z = x.*y gives

2-33 39

If x and y are column vectors, the result of x.*y is a

column vector. For example z = (x’).*(y’) gives

2(–7) –14

z = 4(3) = 12

–5(–8) 40

does not have the same size as y, whose size is 1 × 3.

y.*x’ are not defined in MATLAB and will generate an

error message.

2-34 40

The array operations are performed between the

elements in corresponding locations in the arrays. For

example, the array multiplication operation A.*B results

in a matrix C that has the same size as A and B and has

the elements ci j = ai j bi j . For example, if

A= 11 5 B = –7 8

–9 4 6 2

–9(6) 4(2) –54 8

2-35 41

Dot product

• The sum of the results obtain when multiply two

vector together, element by element. Other name

is scalar product.

• Consider 2 vector; A=[1,2,3] and B=[4,5,6]

• Result of multiplication is y=A.*B

• To get dot product, you need to sum (y) or sum

(A.*B)

• Other alternative by using dot (A,B)

42

Calculate molecular weight

43

• >> dot(AW,Z)

44

The built-in MATLAB functions such as sqrt(x) and

exp(x) automatically operate on array arguments to

produce an array result the same size as the array

argument x.

is base for natural logarithm

the same size as the argument x.

>>y = sqrt(x)

y = >>z=?

2 4 5

2-36 45

However, when multiplying or dividing these

functions, or when raising them to a power,

you must use element-by-element operations if

the arguments are arrays.

you must type

z = exp(y).*sin(x).*(cos(x)).^2.

not the same as the size of y. The result z will

have the same size as x and y.

2-37 46

Find z = exp(a).*sin(c).*(cos(c)).^2

Find z = exp(a).*sin(b).*(cos(b)).^2

Did you get the meaning of why matrix size must agree?

47

Array Division

of array multiplication except that the elements of one

array are divided by the elements of the other array.

Both arrays must have the same size. The symbol for

array right division is ./. For example, if

2-38 48

Also, if

A= 24 20 B = –4 5

–9 4 3 2

C = 24/(–4) 20/5 = –6 4

–9/3 4/2 –3 2

2-39 49

Array Exponentiation

but also to raise scalars and arrays to array powers.

basis, we must use the .^ symbol.

produces the array [33, 53, 83] = [27, 125, 512].

2-40 50

We can raise a scalar to an array power. For example, if

p = [2, 4, 5], then typing 3.^p produces the array

[32, 34, 35] = [9, 81, 243].

is not a decimal point associated with the number 3. The

following operations, with the value of p given here, are

equivalent and give the correct answer:

3.^p

3.0.^p

3..^p

(3).^p

3.^[2,4,5]

2-41 51

52

• x=Force./constant

• p=(constant.*(k.^2))/2

53

Matrix-Matrix Multiplication

columns in A must equal the number of rows in B. The

row-column multiplications form column vectors, and

these column vectors form the matrix result. The

product AB has the same number of rows as A and the

same number of columns as B. For example,

6 –2 9 8 (6)(9) + (– 2)(– 5) (6)(8) + (– 2)(12)

10 3 = (10)(9) + (3)(– 5) (10)(8) + (3)(12)

–5 12

4 7 (4)(9) + (7)(– 5) (4)(8) + (7)(12)

64 24

= 75 116 (2.4–4)

1 116

2-42 54

Use the operator * to perform matrix multiplication in

MATLAB. The following MATLAB session shows how to

perform the matrix multiplication shown in (2.4–4).

>>A = [6,-2;10,3;4,7];

>>B = [9,8;-5,12];

>>A*B

ans =

64 24

75 116

1 116

2-43 55

Matrix multiplication does not have the commutative

property; that is, in general, AB BA. A simple

example will demonstrate this fact:

AB = 6 –2 9 8 = 78 20 (2.4–6)

10 3 –12 14 54 122

whereas

BA = 9 8 6 –2 = 134 6

–12 14 10 3 68 65 (2.4–7)

common and easily made mistake.

2-44 56

57

Special Matrices

null or zero matrix, denoted by 0 and the identity, or

unity, matrix, denoted by I.

The null matrix contains all zeros and is not the same

as the empty matrix [ ], which has no elements.

0A = A0 = 0

IA = AI = A

2-45 58

The identity matrix is a square matrix whose diagonal

elements are all equal to one, with the remaining

elements equal to zero.

I= 1 0

0 1

n × n identity matrix and an identity matrix the same

size as the matrix A.

2-46 59

Sometimes we want to initialize a matrix to have all zero

elements. The zeros command creates a matrix of all

zeros.

whereas typing zeros(m,n) creates an m × n matrix of

zeros.

having the same dimension as the matrix A. This type of

matrix can be useful for applications in which we do not

know the required dimension ahead of time.

that it creates arrays filled with ones.

2-47 60

Polynomial Multiplication and Division

The function conv(a,b) computes the product of the two

polynomials described by the coefficient arrays a and b.

The two polynomials need not be the same degree. The

result is the coefficient array of the product polynomial.

result of dividing a numerator polynomial, whose

coefficient array is num, by a denominator polynomial

represented by the coefficient array den. The quotient

polynomial is given by the coefficient array q, and the

remainder polynomial is given by the coefficient array r.

2-48 61

Polynomial Multiplication and Division: Examples

>>a = [9,-5,3,7];

>>b = [6,-1,2];

>>product = conv(a,b)

product =

54 -39 41 29 -1 14

>>[quotient, remainder] = deconv(a,b)

quotient =

1.5 -0.5833

remainder =

0 0 -0.5833 8.1667

2-49 62

63

Polynomial Roots

polynomial specified by the coefficient array a.

The result is a column vector that contains the

polynomial’s roots.

For example,

r =

-2

-7

2-50 64

Polynomial Coefficients

of the polynomial whose roots are specified by

the vector r. The result is a row vector that

contains the polynomial’s coefficients arranged

in descending order of power.

For example,

c =

1 7 10

2-51 65

Plotting Polynomials

specified values of its independent variable x, which can

be a matrix or a vector. The polynomial’s coefficients of

descending powers are stored in the array a. The result is

the same size as x.

2-52 66

Example of Plotting a Polynomial

2 ≤ x ≤ 5, you type

>>a = [9,-5,3,7];

>>x = [-2:0.01:5];

>>f = polyval(a,x);

>>plot(x,f),xlabel(’x’),ylabel(’f(x)’)

2-53 67

Cell array functions. Table 2.6–1

Function Description

num2cell(A)

deal(A,B, ...) X = A, Y = B, . . . .

deal(A) X = A, Y = A, . . . .

2-54 68

Arrangement of data in the structure array student.

Figure 2.7–1

2-55 69

Structure functions Table 2.7–1

Function Description

names = fieldnames(S) Returns the field names

associated with the

structure array S as names,

a cell array of strings.

F = getfield(S,’field’) Returns the contents of the

field ’field’ in the structure

array S. Equivalent to F =

S.field.

name of a field in the

structure array S, and 0

otherwise.

2-56 70

Structure functions Table 2.7–1 (continued)

S = Removes the field ’field’

rmfield(S,’field’ from the structure array

) S.

Sets the contents of the

field ’field’ to the value

S = V in the structure array

setfield(S,’field S.

’,V)

S = with the fields ’f1’, ’f2’,

struct(’f1’,’v1’, . . . having the values

’f2’,’v2’,...) ’v1’, ’v2’, . . . .

2-57 71

- Matrix 1Uploaded bychandrasmg
- Lecture 3 -Manipulating 2D Objects.pptxUploaded byCik Minn
- Matlab TutorialUploaded bydoebook
- rprogramming-sample.pdfUploaded byFragoso Josué Muhae
- 3. 2018.02.21 Lesson 3 & 4.txtUploaded byAnu Roy
- Properties of Matrix OperationsUploaded byShellie Allaire
- Lecture 1Uploaded byJr Ang
- Modeling Transformations.Uploaded bytarun gehlot
- Illia Civ Quarterl 395 UnivUploaded bytelematico69
- Matlab PracticeUploaded bySalu Zubair
- Chap3_Vectors in MATLABUploaded bySheannen Tan
- Algebra NotesUploaded bynileshgughane
- 2d arraysUploaded byalizarana
- 3 ArraysUploaded byKimuel J MAlizon
- matlab1Uploaded bySalvador Vilca Mamani
- Eamcet Qr Maths Sr Maths Iia-3. MatricesUploaded bysanagavarapu
- Optimal State-Feedback Control Under Sparsity and Delay ConstraintsUploaded bynaumz
- pp reviewUploaded bySunil Chowdary
- Discrete Image ProcessingUploaded byMahesh Abnave
- Www Sanfoundry ComUploaded byanis ansari
- Chapter_09.pdfUploaded bySyed Uzair
- WBDev FunctionsUploaded byAnonymous Y2ibaULes1
- presentation matrixUploaded byapi-280449798
- Sukatan Pelajaran Maths S STPMUploaded byKar Wai Ng
- practical guideline 2014-15.pdfUploaded byDevansh
- Slides Assignment2 ReviewUploaded bymounamalar
- BlablaUploaded byMil
- Num Methods in CM Cor 2012-01-23Uploaded byJoseAlfredoCarneiroDosSantos
- 1001_SDR_umts_radio_20190618113906Uploaded byNguyen Quang Khai
- 1312.3860Uploaded byWalid El-Shafai

- Interval Arithmetic.psUploaded bytrabajados
- Archival Management Software Spiro_Jan13Uploaded byxmetal1951
- CastrolUploaded byKing Nitin Agnihotri
- Public-private Partnerships as an Element of Public Procurement Reform in GermanyUploaded byDeyana Marcheva
- observation 1Uploaded byapi-255571079
- flow in filmUploaded byCynthia Robinson
- MPE Mekong EIA Briefing FinalUploaded bygina tumlos
- 10.1.1.559.8529Uploaded byGherghe Bogdan
- Soil Analytical Methods.docxUploaded byLucky Khan
- dmeUploaded byBikram Bangarh
- Donald Worster the Ends of the EarthUploaded byJoel Chatuki
- todiniUploaded byEdwin Aranda Saldaña
- sample.pdfUploaded bySandeep Chaurasia
- Fault List.pdfUploaded byAristóteles Terceiro Neto
- Rushkoff_ Douglas - Program or Be Programmed. Ten Commands for a Digital AgeUploaded byМyrоslаw Бytz
- Analysis of Jaws,Frankenstien,TerminatorUploaded bySGmediastudies
- Operations Manager 101Uploaded byBilicic
- Buora-Roberto 2010Uploaded bymbuora
- Install Monitorix Di Debian 6 SqueezeUploaded byPendekar Blank
- Burning Woman - Disfunkshion MagazineUploaded byTheOwlReads
- Bipul Kumar ResumeUploaded bySujot Tumbade
- assignment 4 javaUploaded byHarish Upmanyu
- Constructivism and PBLUploaded byHortensia Jiménez Díaz
- PEX-01-02(for group 2) (1)Uploaded byEarle Jimenez Niervo RN
- The Development of the Geriatric Index of Communicative Ability (GICA) for Measuring Communicative Competence of Elderly_ a Pilot StudyUploaded byMauricio Escobar Castañeda
- e1447_p4s800-mxUploaded byFernando Cores Abalo
- leadership theories and the development of nurses in primary health careUploaded byNoor Rashidah Baharuddin
- Blueprint for Learning ConstructionUploaded byRose
- ITE6 Instructor Materials Chapter1Uploaded byhavisu
- Distance Time Velocity SemUploaded byFernando Gonzalez