Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the...

44
Manipulating MATLAB Vector, Matrices 1

description

An array is MATLAB's basic data structure. Can have any number of dimensions. Most common are: vector - one dimension (a single row or column) matrix - two or more dimensions Arrays can have numbers or letters. (Arrays) 3

Transcript of Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the...

Page 1: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Manipulating MATLAB

Vector, Matrices

1

Page 2: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Variables and ArraysWhat are variables?

You name the variables (as the programmer) and assign them numerical values.

You execute the assignment command to place the variable in the workspace memory (memory is part of hardware used for storing information).

You are allowed to use the variable in algebraic expressions, etc. once it is assigned.

2

Page 3: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

An array is MATLAB's basic data structure.

Can have any number of dimensions. Most common are:

vector - one dimension (a single row or column)

matrix - two or more dimensionsArrays can have numbers or letters.

(Arrays)

3

Page 4: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

In MATLAB, a variable is stored as an array of numbers. When appropriate, it is interpreted as a scalar, vector or matrix.

The size of an array is specified by the number of rows and the number of columns in the array, with the number of rows indicated first.

Variables as ArraysVariables as Arrays

scalar1 × 1

vectorn × 1 or 1 × n

matrixn × m

4

Page 5: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

• Scalars are 1×1 arrays.• They contain a single value, for example:

• X=3• Height=5.34• Width=10.09

Scalars

5

Page 6: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

VectorsA vector is a list of numbers expressed as a 1

dimensional array.A vector can be n×1 or 1×n.Columns are separated by commas (or spaces):x = [1, 2, 3] or x = [1 2 3]

Rows are separated by semicolons:y= [1; 2; 3]

6

Page 7: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

To create a row vector from known numbers, type variable name, then equal sign, then inside square brackets, numbers separated by spaces.

variable_name = [ n1, n2, n3 ]

>> yr = [1984 1986 1988 1990 1992 1994 1996]

yr =

1984 1986 1988 1990 1992 1994 1996

Note MATLAB displays row vector horizontally

Commas optional

(Creating a row vector)

7

Page 8: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

To create a column vector from known numbers

Method 1 - same as row vector but put semicolon after all but last number

variable_name = [ n1; n2; n3 ]

>> yr = [1984; 1986; 1988 ]

yr =

1984

1986

1988

Note: MATLAB displays column vector vertically

(Creating a Column Vector)

8

Page 9: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Method 2 - same as row vector but put apostrophe (') after closing bracket.Apostrophe interchanges rows and

columns. Will come back on this later.variable_name = [ n1 n2 n3 ]'

>> yr = [1984 1986 1988 ]'

yr =

1984

1986

1988

(Creating a column vector)

9

Page 10: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

To create a vector with specified constant spacing between elements

variable_name = m:q:n

m is the first numbern is the last numberq is the difference between consecutive

numbersv = m:q:n

meansv = [ m m+q m+2q m+3q ... n ]

(Constant spacing vectors)

10

Page 11: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

If q is omitted, the spacing is 1v = m:n

meansv = [ m m+1 m+2 m+3 ... n ]

>> x = 1:2:13x = 1 3 5 7 9 11 13>> y = 1.5:0.1:2.1y = 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000

Non-integer spacing

(Constant spacing vectors)

11

Page 12: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

>> z = -3:7

z = -3 -2 -1 0 1 2 3 4 5 6 7

>> xa = 21:-3:6

xa = 21 18 15 12 9 6

>> fred = 7:-2:15

fred = Empty matrix: 1-by-0

Negative spacing

(Constant spacing vectors)

Impossible spacing

12

Page 13: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

1. How would you use linspace to set up spacings for planting seedlets in a garden row (30 seedlets, each row 2 meters long).

2. How about planning a 4285km road trip on 21 days. How long each leg would be?

(Quick linspace Exercises)

13

Page 14: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

To create a vector with specified number of terms between first and last

v = linspace( xi, xf, n )xi is first numberxf is last numbern is the number of terms (obviously must be a positivenumber) (= 100 if omitted)

(Fixed length vectors)

14

Page 15: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

>> va = linspace( 0, 8, 6 )

va = 0 1.6000 3.2000 4.8000 6.4000 8.0000

>> va = linspace( 30, 10, 11 )

va=30 28 26 24 22 20 18 16 14 12 10

m:q:n lets you directly specify spacing. linspace() lets you directly specify number of terms.

Six elements

Decreasing elements

T I P

15

Page 16: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Indexing Into an Array allows you to change a value

Page 17: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Adding Elements

Page 18: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

If you add an element outside the range of the original array, intermediate elements are added with a value of zero

Page 19: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

2-D Matrices can also be entered by listing each row on a separate line C = [-12, 0, 0

12, 13, 40 1, -1, 90 0, 0, 2]

2-D Matrices

Page 20: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

M = [12, 152, 6, 197, 32, -32, … 55, 82, 22, 10];

Use an ellipsis to continue a definition onto a new line

2-D Matrices

Page 21: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

2-D matrix

These semicolons are optional

Page 22: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Define a matrix using other matrices as components

Page 23: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Or…

Page 24: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

MatricesA matrix is a two

dimensional array of numbers.

For example, this is a 4×3 matrix:

mat1=[1.0,1.8,1.6; 3.0,-2.0,25.1; 0.0,-5.1,12.7; 2.3,0.3,-3.1]

11 22 33

11 1.01.0 1.81.8 1.61.6

22 3.03.0 -2.0-2.0 25.25.11

33 0.00.0 -5.1-5.1 12.12.77

44 2.32.3 0.30.3 -3.1-3.1

Columns

Row

s

24

Page 25: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Indexed-location of numbers in an arrayEach item in an

array is located in the (row, column).

mat1(2,3)ans= 25.1000

11 22 33

11 1.01.0 1.81.8 1.61.6

22 3.03.0 -2.0-2.0 25.25.11

33 0.00.0 -5.1-5.1 12.12.77

44 2.32.3 0.30.3 -3.1-3.1

Columns

Row

s

25

Page 26: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Enter the following into MATLAB:Scalar:

x=90

Vectors:y=[1, 0, 55]

z=[1 0 55]

Matrix: m= [ 55, 44, 33; 0, 2, 88]

Practice!

26

Page 27: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Vector, Matrices

27

Manipulating MATLAB

Page 28: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Defining (or assigning) arrays

28

An array can be defined by typing in a list of numbers enclosed in square brackets:

Commas or spaces separate numbers.m=[12, 1, -33] or m=[12 1 -33]

Semicolons indicate a new row.

n=[2, 50, 20;1,1,2;0,-2,66]

m=m= 12 18 -3312 18 -33

n =n = 2 50 20 2 50 20 1 1 21 1 2 0 -2 660 -2 66

Page 29: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

p =p = 12 18 -3312 18 -33 12 18 -3312 18 -33 2 50 202 50 20 2 50 202 50 20 1 1 21 1 2 1 1 21 1 2 0 -2 660 -2 66 0 -2 660 -2 66

Defining arrays continued

29

You can define an array in terms of another array:r=[m;n]

p=[r,r]

r =r = 12 18 -3312 18 -33 2 50 20 2 50 20 1 1 21 1 2 0 -2 660 -2 66

Page 30: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

We can access (read from or write to) elements in an array (vector or matrix) individually or in groups.

• Useful for changing a subset of elements.

• Useful for making a new variable from a subset of elements.

(Verctor/Matrix Addressing)

30

Page 31: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(Accessing Vectors Elements 1)

31

You can access any element by indexing the vector name with parentheses (indexing starts from 1, not from 0 as in C).

>> odd = [1:3:10] odd = 1 4 7 10

>> odd(3) ans = 7

Page 32: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Review…

32

Index – a number used to identify elements in an arrayRetrieving a value from an array:

n(2,1) ans=1

You can change a value in an element in an array with indexing: n(3,2)=55

n =n = 2 50 20 2 50 20 1 1 21 1 2 0 -2 660 -2 66

n =n = 2 50 20 2 50 20 1 1 21 1 2 0 55 660 55 66

You can extend an array by defining a new element:

m=[12 1 -33] m(6)=9 m=[12 1 -33 0 0 9]

Notice how undefined values of the array are filled with zeros

Page 33: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

33

We can also access a range of elements (a subset of the original vector) by using the colon operator and giving a starting and ending index.

>> odd(2:4) ans = 4 7 10

Simple arithmetic between scalars and vectors is possible.

>> 10 + [1 2 3] ans = 11 12 13

(Range of Elements / Vector Arithmetic)

Page 34: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(Matrices)

34

Create a two-dimensional matrix like this m = [ row 1 numbers; row 2 numbers; ... ; last row numbers ]

Each row separated by semicolon. All rows have same number of columns

>> a=[ 5 35 43; 4 76 81; 21 32 40]

a =

5 35 43

4 76 81

21 32 40

Page 35: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(Matrices)

35

>> cd=6; e=3; h=4;

>> Mat=[e, cd*h, cos(pi/3);... h^2 sqrt(h*h/cd) 14]

Mat =

3.0000 24.0000 0.5000

16.0000 1.6330 14.0000

Comma is optional

Page 36: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(Matrices)

36

Can also use m:p:n or linspace() to make rowsMake sure each row has same number of

columns>> A=[1:2:11; 0:5:25;... linspace(10,60,6); 67 2 43 68 4 13]

A =

1 3 5 7 9 11

0 5 10 15 20 25

10 20 30 40 50 60

67 2 43 68 4 13

Page 37: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(Matrices)

37

What if the number of columns is different?

>> B= [ 1:4; linspace(1,4,5) ]

??? Error using ==> vertcat

CAT arguments dimensions are not consistent.

All arrys in the argument list must have the same number of columns.

Four columns

Five columns

Page 38: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(Special Matrix Commands)

38

zeros(m,n) - makes a matrix of m rows and n columns, all with zeros.

ones(m,n) - makes a matrix of m rows and n columns, all with ones.

eye(n) - makes a square matrix of n rows and columns. Main diagonal (upper left to lower right) has ones, all other elements are zero.

Page 39: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Examples!

39

zeros(2,5) ans = 0 0 0 0 0 0 0 0 0 0

ones(3,5) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

eye(5) ans =

1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1

Note: Placing a single number inside either function will return an n × n array. e.g. ones(5) will return a 5 × 5 array filled with ones.

Page 40: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

>> zr=zeros(3,4)

zr = 0 0 0 0

0 0 0 0

0 0 0 0

>> ne=ones(4,3)

ne = 1 1 1

1 1 1

1 1 1

1 1 1

>> idn=eye(5)

idn = 1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

40

Page 41: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

To make a matrix filled with a particular number, multiply ones(m,n) by that number

>> z=100*ones(3,4)

z =

100 100 100 100

100 100 100 100

100 100 100 100

T I P

41

Page 42: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(About variables in MATLAB)

42

All variables are arraysScalar - array with only one elementVector - array with only one row or columnMatrix - array with multiple rows and columns

Assigning to variable specifies its dimensionDon't have to define variable size before assigning

to it, as you do in many programming languagesReassigning to variable changes its dimension to that

of assignment.

Page 43: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

(The Transpose Operator)

43

Transpose a variable by putting a single quote after it, e.g., x'In math, transpose usually denoted by

superscript "T", e.g., xTConverts a row vector to a column vector and vice-versa.

Switches rows and columns of a matrix, i.e., first row of original becomes first column of transposed, second row of original becomes second column of transposed, etc.

Page 44: Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.

Continue…

44

The transpose operator, an apostrophe, changes all of an array’s rows to columns and columns to rows.m = [12, 1, -33] m’

m= ans= 12 1 -33 12 1 -33

>> aa=[3 8 1]

aa = 3 8 1

>> bb=aa'

bb = 3

8

1