Docsity

Arrays-and-Script matlab notes, Slides of Matlab skills

Partial preview of the text

Download Arrays-and-Script matlab notes and more Slides Matlab skills in PDF only on Docsity!

Lecture 3: Array Applications,Cells, Structures

Script Files

Dr. Mohammed Hawa

Electrical Engineering Department

University of Jordan

EE201: Computer Applications. See Textbook Chapter 2 and Chapter 3.

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Euclidean Vectors

•^

An Euclidean vector (or geometric vector, orsimply a vector) is a geometric entity that hasboth

magnitude

and

direction

•^

In physics, vectors are used to representphysical quantities that have both magnitudeand direction, such as force, acceleration,electric field, etc.

•^

Vector algebra: adding and subtractingvectors, multiplying vectors, scaling vectors,etc.

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Magnitude, Length, Absolute Value •^

In MATLAB,

length()

of a

vector is

not

its magnitude.

It is the number of elementsin the vector.

-^

The

absolute value

of a

vector

a

is a vector whose

elements are the absolutevalues of the elements of

a

-^

The

magnitude

of a vector is

its Euclidean norm orgeometric length as shown:

᠁^

⡰^ げ

‖^ =

䙰^2

−^4

2 −^45

㐳^ =

>> a

[2,

5]

a^

>> length(a)ans =

>> abs(a)ans =

>> sqrt(a*a')

magnitude

ans =

>> sqrt(sum(a.*a))

%magnitude

ans =

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Vector Scaling

•^

For vector:^ ᠁ 㐄 ᡓ

•^

Scaling this vector bya factor of 2 gives:

•^

•^

This is just likeMATLAB scalarmultiplication of avector:

•^

v = 2*[x, y, z];

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Exercise

a

=

[2 -

6]

a^

=

2

6

b

=

[3 -

-1]

b^

=

3

c

=

a +

b

c^

=

5

5

d

=

a -

b

d^

=

7

e

=

2*a

e^

=

4

12

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Dot Product

•^

The dot product ofvectors results in ascalar value.

•^

け^

げ

こ

cos

a

=

[

-4 6];

b

=

[

-1 -1];

c

=

a

b'

c^

=

4

c

=

sum(a

.*

b)

c^

=

4

c

=

dot(a,

b)

c^

=

4

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

ComplexNumbers

a =

7 +

4j

a =

7.0000 +

4.0000i

[theta,

rho]

= cart2pol(real(a),

imag(a))

theta =

rho

= 8.

rho

= abs(a)

% magnitude

of complex number

rho

= 8.

theta =

atan2(imag(a), real(a))

theta =

% atan

is

four

quadrant inverse

tangent

b =

3 +

4j

b =

3.0000 +

4.0000i

a+b ans

= 10.0000 +

8.0000i

a*b ans

= 5.0000 +

40.0000i

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Polynomials

•^

A polynomial can be written in the form:^ ᡓ

ぁ

ぁ

ぁ⡹⡩

ぁ⡹⡩

⡰

⡰^

⡩

⡨

•^

Or more concisely:

〶

ぁ 〶⢀⡨

•^

We can use MATLAB to find all the rootsof the polynomial, i.e., the values of

x

that

makes the polynomial equation equal 0.

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Just for fun… Plot…

x = -2:0.01:5;>> f =

x.^

  • 7*(x.^2)
  • 40*x - 34;

plot(x, f)

-^ -^

0

1

2

3

4

150 100 50 0 -50 -100 -

13

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Cell Array

•^

The cell array is an array in which eachelement is a cell. Each cell can contain anarray.

•^

So, it is an array of different arrays.

•^

You can store different classes of arrays ineach cell, allowing you to group data setsthat are related but have differentdimensions.

•^

You access cell arrays using the sameindexing operations used with ordinaryarrays, but using

not

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Exercise

C = cell(3)C^

=

[]

[]

[]

[]

[]

[]

[]

[]

[]

D = cell(1, 3)D^

=

[]

[]

[]

A(1,1) =

{'Walden

Pond'};

A(1,2) =

{[1+2i 5+9i]};

A(2,1) =

{[60,72,65]};

A(2,2) =

{[55,57,56;54,56,55;52,55,53]};

AA^

=

'Walden

Pond'

[1x2 double]

[1x

double]

[3x3 double]

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Exercise (Continue)

celldisp(A) A{1,1}

=

Walden

Pond

A{2,1}

= 60

72

65

A{1,2}

=

  • 2.0000i
  • 9.0000i

A{2,2}

= 55

57

56

54

56

55

52

55

53

B =

{[2,4],

[6,-9;3,5];

[7;2],

10}

B^

=

[1x

double]

[2x2 double]

[2x

double]

[^

10]

B{1,2} ans

=

6

  • 3

5

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Create and Add to Structure

student.name

= 'John

Smith';

student.SSN

=

'392-77-1786';

student.email

= '[email protected]';

student.exam_scores =

[67,75,84];

student student

=

name:

'John Smith' SSN:

'392-77-1786'

email:

'[email protected]'

exam_scores:

[

75

84]

student(2).name =

'Mary Jones';

student(2).SSN =

'431-56-9832';

student(2).email

= '[email protected]';

student(2).exam_scores

= [84,78,93];

student student

=

1x2 struct

array

with fields:

nameSSNemail exam_scores

Copyright © Dr. Mohammed Hawa

Electrical Engineering Department, University of Jordan

Investigate Structure

student(2) ans

name:

'Mary

Jones'

SSN:

email:

'[email protected]'

exam_scores:

[
93]

fieldnames(student) ans

= 'name''SSN''email''exam_scores'

max(student(2).exam_scores) ans

=^93

isstruct(student) ans