Download presentation

Presentation is loading. Please wait.

1
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating arrays © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

2
Variables and Arrays What are variables? 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.

3
Variable Naming Rules Must begin with a LETTER Must begin with a LETTER May only contain letters, numbers and underscores ( _ ) May only contain letters, numbers and underscores ( _ ) No spaces or punctuation marks allowed! No spaces or punctuation marks allowed! Only the first 63 characters are significant; beyond that the names are truncated. 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) Case sensitive (e.g. the variables a and A are not the same)

4
Which variable names are valid? 12oclockRock 12oclockRock tertiarySector tertiarySector blue cows blue cows Eiffel65 Eiffel65 red_bananas 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) 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)

5
Variable Naming Conventions There are different ways to name variables. The following illustrate some of the conventions used: 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: 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. It does not matter which convention you choose to work with; it is up to you.

6
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 Arrays scalar 1 × 1 vector n × 1 or 1 × n matrix n × m

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

8
Vectors A vector is a list of numbers expressed as a 1 dimensional array. A vector is a list of numbers expressed as a 1 dimensional array. A vector can be n×1 or 1×n. A vector can be n×1 or 1×n. Columns are separated by commas (or spaces): Columns are separated by commas (or spaces): Rows are separated by semicolons: Rows are separated by semicolons:

9
Matrices A matrix is a two dimensional array of numbers. A matrix is a two dimensional array of numbers. For example, this is a 4×3 matrix: For example, this is a 4×3 matrix:12313.01.83.6 24.6-2.021.3 30.0-6.112.8 42.30.3-6.1 Columns Rows

10
Indexed-location of numbers in an array Each item in an array is located in the (row, column). Each item in an array is located in the (row, column).12313.01.83.6 24.6-2.021.3 30.0-6.112.8 42.30.3-6.1 Columns Rows ans = 21.3000 21.3000

11
Enter the following into MATLAB: Enter the following into MATLAB: –Scalar: –Vectors: –Matrix: Hands-on

12
Hands-on Enter (input) the following matrix into MATLAB: Enter (input) the following matrix into MATLAB: -7216 2320 -50-18.5 whiteRabbit =

13
Scalar Operations Operation Algebraic Syntax MATLAB Syntax Addition a + b Subtraction a - b a – b Multiplication a × b a.* b Division a ÷ b a./ b Exponentiation abababab a.^ b

14
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. 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: To deal with arrays on an element-by-element level we need to use the following array or dot-operators:.*,./ and.^

15
Array operations & dot-operators Because scalars are equivalent to a 1×1 array, you can either use the standard or the dot-operators when doing multiplication, division and exponentiation of scalars (i.e., of single numbers). Because scalars are equivalent to a 1×1 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 dot- operators, unless you intend to perform vector or matrix multiplication or division. It is okay for you to always use the dot- operators, unless you intend to perform vector or matrix multiplication or division..*,./ and.^

16
Example: Example: 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! (.*./.^) Array vs. Matrix Operations

17
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 Multiplication & Division, and then Addition & Subtraction last. Addition & Subtraction last. An example: c = 2+3^2+1/(1+2)1 st c = 2+3^2+1/3 c = 2+3^2+1/(1+2)2 nd c = 2+9+1/3 c = 2+3^2+1/(1+2)3 rd c = 2+9+0.33333 c = 2+3^2+1/(1+2)4 th c = 11+0.33333 c = 2+3^2+1/(1+2)5 th c = 11.33333

18
Hands-on Enter these two arrays into MATLAB: Enter these two arrays into MATLAB: Multiply, element-by-element, a × b. Multiply, element-by-element, a × b. –Since this is an array operation, the.* multiplication operation is implied by the request. a = 10 5 5 10 5 5 2 9 0 2 9 0 6 8 8 6 8 8 b = 1 0 2 1 0 2 0 0 0 0 0 0 1 1 0 1 1 0

19
Defining & manipulating arrays All variables in MATLAB are arrays! All variables in MATLAB are arrays! –Single number array & scalar: 1 × 1 –Row array & row vector: 1 × n –Column array & column vector: n x 1 –Array of n rows x m columns & Matrix: n × m –Naming rules –Indexed by (row, column) Remark: vectors and matrices are special mathematical objects, arrays are lists or tables of numbers. Remark: vectors and matrices are special mathematical objects, arrays are lists or tables of numbers.

20
The equal sign assigns Consider the command lines: 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. 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.

21
An array can be defined by typing in a list of numbers enclosed in square brackets: An array can be defined by typing in a list of numbers enclosed in square brackets: –Commas or spaces separate numbers. –Semicolons indicate a new row. Defining (or assigning) arrays A = 12 18 -3 12 18 -3 B = 2 5 2 2 5 2 1 1 2 1 1 2 0 -2 6 0 -2 6

22
D = 12 18 -3 12 18 -3 12 18 -3 12 18 -3 2 5 2 2 5 2 2 5 2 2 5 2 1 1 2 1 1 2 1 1 2 1 1 2 0 -2 6 0 -2 6 0 -2 6 0 -2 6 Defining arrays continued You can define an array in terms of another array: You can define an array in terms of another array: C = C = 12 18 -3 12 18 -3 2 5 2 2 5 2 1 1 2 1 1 2 0 -2 6 0 -2 6

23
Create an array of zeros: Create an array of zeros: Create an array of ones: Create an array of ones: 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. Creating Zeros & Ones arrays E = 0 0 0 0 0 0 0 0 0 0 F = 1 1 1 1 1 1

24
Index – a number used to identify elements in an array Index – a number used to identify elements in an array –Retrieving a value from an array: Retrieving Values in an Array ans = 4 ans = 8 G = 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9

25
You can change a value in an element in an array with indexing: You can change a value in an element in an array with indexing: You can extend an array by defining a new element: You can extend an array by defining a new element: –Notice how undefined values of the array are filled with zeros Changing Values in an Array A = 12 5 -3 12 5 -3 A = 12 5 -3 0 0 8 12 5 -3 0 0 8

26
Colon notation can be used to define evenly spaced vectors in the form: Colon notation can be used to define evenly spaced vectors in the form: first : last The default spacing is 1, so to use a different increment, use the form: The default spacing is 1, so to use a different increment, use the form: first : increment : last first : increment : last –The numbers now increment by 2 Colon Operator H = 1 2 3 4 5 6 1 2 3 4 5 6 I = 1 3 5 7 9 11 1 3 5 7 9 11

27
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. The colon represents an entire row or column when used in as an array index in place of a particular number. G = 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 ans = 1 4 7 3 6 9 4 5 6 4 5 6

28
Extracting Data with the Colon Operator Continued The colon operator can also be used to extract a range of rows or columns: The colon operator can also be used to extract a range of rows or columns: G = 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 ans = 2 3 2 3 G = 4 5 6 4 5 6 7 8 9 7 8 9

29
Manipulating Arrays The transpose operator, an apostrophe, changes all of an array’s rows to columns and columns to rows. The transpose operator, an apostrophe, changes all of an array’s rows to columns and columns to rows. J = 1 3 7 1 3 7 ans = 1 3 7

30
Manipulating Matrices Continued The functions fliplr() and flipud() flip a matrix left-to-right and top-to-bottom, respectively. 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.

31
Hands-on exercise: Create the following matrix using colon notation: Create the following matrix using colon notation: W = 1 2 3 4 5 1 2 3 4 5 10 12 14 16 18 10 12 14 16 18 6 5 4 3 2 6 5 4 3 2 All three rows are evenly spaced All three rows are evenly spaced –The first row ranges from 1 to 5 in increments of 1 1:5 –The second row ranges from 10 to 18 in increments of 2 10:2:18 –The third row ranges from 6 to 2 in increments of -1 6:-1:2 –All together:

32
Hands-on continued Create the following matrix using colon notation: Create the following matrix using colon notation: X = 1.2 2.3 3.4 4.5 5.6 1.2 2.3 3.4 4.5 5.6 1.9 3.8 5.7 7.6 9.5 1.9 3.8 5.7 7.6 9.5 0 -3 -6 -9 -12 0 -3 -6 -9 -12 Transpose this matrix and assign it to variable Y. Transpose this matrix and assign it to variable Y. >> Y = x’ Extract the 2nd row from Y and assign it to variable Z. Extract the 2nd row from Y and assign it to variable Z. >> Z = Y(2,:)

33
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). 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. Arrays are rows and columns of numbers. Array operations (element-by-element operations with the dot-operators) Array operations (element-by-element operations with the dot-operators) Hierarchy of arithmetic operations. Hierarchy of arithmetic operations.

34
Summary (2 of 2) Command lines that assign variables numerical values start with the variable name followed by = and then the defining expression 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. 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. Array, vector and matrix operations are efficient MATLAB computational tools.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google