Anda di halaman 1dari 34

MATLAB fundamentals

Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating arrays

Lecture 2

Variables and Arrays

What are variables?

Variables are arrays of numbers. 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.

Variable Naming Rules


Must begin with a LETTER May only contain letters, numbers and underscores ( _ ) No spaces or punctuation marks allowed! Only the first 63 characters are significant; beyond that the names are truncated. Case sensitive (e.g. the variables a and A are not the same)

Which variable names are valid?

12oclockRock tertiarySector blue cows Eiffel65 red_bananas This_Variable_Name_Is_Quite_Possibly_Too_Lo ng_To_Be_Considered_Good_Practice_However _It_Will_Work % (the green part is not part of the recognized name)

Variable Naming Conventions

There are different ways to name variables. The following illustrate some of the conventions used: lowerCamelCase UpperCamelCase underscore_convention If a variable is a constant, some programmers use all caps: CONSTANT It does not matter which convention you choose to work with; it is up to you.

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

scalar
11

vector
n 1 or 1 n

matrix
nm

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.

Scalars
Scalars are 11 arrays. They contain a single value, for example:

r = 6 height = 5.3 width = 9.07

Vectors

A vector is a list of numbers expressed as a 1 dimensional array. A vector can be n1 or 1n.

Columns are separated by commas (or spaces): h = [1, 2, 3]

Rows are separated by semicolons:


v = [1; 2; 3]

Matrices

Columns
1
1 2 3 4 3.0 4.6 0.0 2.3

2
1.8 -2.0 -6.1 0.3

3
3.6 21.3 12.8 -6.1

A matrix is a two dimensional array of numbers. For example, this is a 43 matrix:


Rows

m = [3.0, 1.8, 3.6; 4.6, -2.0, 21.3; 0.0, -6.1, 12.8; 2.3, 0.3, -6.1]

Indexed-location of numbers in an array

Each item in an array is located in the (row, column).


m(2,3) ans = 21.3000
Rows

Columns
1 1 2 3 4 3.0 4.6 0.0 2.3 2 1.8 -2.0 -6.1 0.3 3 3.6 21.3 12.8 -6.1

Hands-on

Enter the following into MATLAB:


Scalar:
a = 1

Vectors:
b = [1, 0, 2] c = [1 0 2]

Matrix:
d = [5, 4, 3; 0, 2, 8]

Hands-on

Enter (input) the following matrix into MATLAB:

-7
whiteRabbit =

21 32

6 0

-5

-18.5

Scalar Operations
Operation
Addition Subtraction Multiplication Division Exponentiation

Algebraic Syntax

MATLAB Syntax

a+b a-b ab ab ab

a + b a b a .* b a ./ b a .^ b

Array Operations

Arrays of numbers in MATLAB can be interpreted as vectors and matrices if vector or matrix algebra is to be applied. Recall that matrices are mathematical objects that can be multiplied by the rules of matrices. To do matrix multiplication, you need to use the standard *, /, and ^ operators [without the preceding . (dot)]. They are not for array multiplication, division and exponentiation. To deal with arrays on an element-by-element level we need to use the following array or dot-operators:

.*

./

and

.^

Array operations & dot-operators


.*

./

and

.^

Because scalars are equivalent to a 11 array, you can either use the standard or the dot-operators when doing multiplication, division and exponentiation of scalars (i.e., of single numbers). It is okay for you to always use the dotoperators, unless you intend to perform vector or matrix multiplication or division.

Array vs. Matrix Operations

Example:

x = [2, 1; 3, 4] y = [5, 6; 7, 8]

z = x .* y results in [10, 6; 21, 32]; this is array multiplication z = x * y results in [17, 20; 43, 50]; this is matrix multiplication So, do NOT forget the dot when doing array operations! (.* ./ .^)

Hierarchy of Operations
Just like in mathematics the operations are done in the following order: Left to right doing what is in Parentheses & Exponents first, followed by Multiplication & Division, and then Addition & Subtraction last.
An example:

c c c c c

= = = = =

2+3^2+1/(1+2) 2+3^2+1/(1+2) 2+3^2+1/(1+2) 2+3^2+1/(1+2) 2+3^2+1/(1+2)

1st 2nd 3rd 4th 5th

c c c c c

= = = = =

2+3^2+1/3 2+9+1/3 2+9+0.33333 11+0.33333 11.33333

Hands-on

Enter these two arrays into MATLAB:


a = 10 2 6 5 9 8 5 0 8 b = 1 0 1 0 0 1 2 0 0

Multiply, element-by-element, a b.
Since this is an array operation, the .* multiplication operation is implied by the request.

Defining & manipulating arrays

All variables in MATLAB are arrays!


Single number array & scalar: Row array & row vector: Column array & column vector: Array of n rows x m columns & Matrix: Naming rules Indexed by (row, column) 11 1n nx1 nm

Remark: vectors and matrices are special mathematical objects, arrays are lists or tables of numbers.

The equal sign assigns

Consider the command lines:


>> >> >> >> ax = 5; bx = [1 2]; by = [3 4]; b = bx + by;

The equal sign (=) commands that the number computed on the right of it is input to the variable named on the left; thus, it is an assignment operation.

Defining (or assigning) arrays

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


Commas or spaces separate numbers.
A = [12, 18, -3] or A = 12 18 -3 A = [12 18 -3]

Semicolons indicate a new row. B = [2, 5, 2; 1 , 1, 2; 0, -2, 6] B = 2 5 2 1 1 2 0 -2 6

Defining arrays continued

You can define an array in terms of another array:


C = [A; B] C = 12 18 2 5 1 1 0 -2
D = [C, C] D = 12 18 2 5 1 1 0 -2

-3 2 2 6

-3 2 2 6

12 2 1 0

18 5 1 -2

-3 2 2 6

Creating Zeros & Ones arrays

Create an array of zeros:


E = zeros(3,5) E = 0 0 0 0 0 0

0 0 0

0 0 0

0 0 0

Create an array of ones:


F = ones(2,3) F = 1 1 1 1

1 1

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

Retrieving Values in an Array

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

G = [1, 2, 3; 4, 5, 6; 7, 8, 9] G = 1 2 3 4 5 6 7 8 9
G(2,1) ans = 4 G(3,2) ans = 8

Changing Values in an Array

You can change a value in an element in an array with indexing:

A(2) = 5 A = 12

-3

You can extend an array by defining a new element:

A(6) = 8 A = 12

-3

Notice how undefined values of the array are filled with zeros

Colon Operator

Colon notation can be used to define evenly spaced vectors in the form:

first : last
H = 1:6 H = 1

The default spacing is 1, so to use a different increment, use the form:

first : increment : last

I = 1:2:11 I = 1 3

11

The numbers now increment by 2

Extracting Data with the Colon Operator

The colon represents an entire row or column when used in as an array index in place of a particular number.
G = 1 4 7 2 5 8 3 6 9

G(:,1) ans = 1 4 7

G(:,3) ans = 3 6 9

G(2,:) ans = 4

Extracting Data with the Colon Operator Continued

The colon operator can also be used to extract a range of rows or columns:
G = 1 4 7 2 5 8 3 6 9

G(2:3,:) G = 4 7

5 8

6 9

G(1,2:3) ans = 2

Manipulating Arrays

The transpose operator, an apostrophe, changes all of an arrays rows to columns and columns to rows.
J = [1 , 3, 7] J = 1 3

J' ans =
7 1 3 7

Manipulating Matrices Continued

The functions fliplr() and flipud() flip a matrix left-to-right and top-to-bottom, respectively.
Experiment with these functions to see how they work.

Hands-on exercise:

Create the following matrix using colon notation:


W = 1 10 6 2 12 5 3 14 4 4 16 3 5 18 2

All three rows are evenly spaced


1:5 10:2:18 6:-1:2

The first row ranges from 1 to 5 in increments of 1 The second row ranges from 10 to 18 in increments of 2

The third row ranges from 6 to 2 in increments of -1

All together: W = [1:5; 10:2:18; 6:-1:2]

Hands-on continued

Create the following matrix using colon notation:


X = 1.2 1.9 0 2.3 3.8 -3 3.4 5.7 -6 4.5 7.6 -9 5.6 9.5 -12

Transpose this matrix and assign it to variable Y. >> Y = x Extract the 2nd row from Y and assign it to variable Z. >> Z = Y(2,:)

Summary (1 of 2)
Naming a variable: Start with letter followed by any combination of letters, numbers and underscores (up to 63 of these objects are recognized). Arrays are rows and columns of numbers. Array operations (element-by-element operations with the dot-operators) Hierarchy of arithmetic operations.

Summary (2 of 2)
Command lines that assign variables numerical values start with the variable name followed by = and then the defining expression An array of numbers is the structure of variables in MATLAB. Within one variable name, a set of numbers can be stored. Array, vector and matrix operations are efficient MATLAB computational tools.

Anda mungkin juga menyukai