Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers."— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers

2 Lecture Outline Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers MATLAB Basics  Variables and arrays Slide 2 of 14

3 MATLAB basics Variables and arrays: Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers A variable is a symbolic name (or identifier) which serves as a placeholder or container for “data”  It is good programming practice to use a pertinent name for variables o E.g., >> accel_due_to_gravity = 9.81;% g in m/s^2  Variables exhibit a variety of properties o Scope or visibility – Is it globally or only locally visible? o Extent – Life-time of the variable? o Type – Is it an integer, float, character, … Slide 3 of 14

4 MATLAB basics Variables and arrays: Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers Strongly typed languages  Requires that the data type of the variable must be declared before it is used o E.g., int miles; – The variable “miles” is declared to be of type integer Weakly typed languages  Does NOT Requires that the data type of the variable be declared before it is used o E.g., >> letters = ‘ab’; – The variable “letters ” is determined to be of type character because of the type of data assigned to the variable. C programming language MATLAB programming language Slide 4 of 14

5 MATLAB basics Variables and arrays – Matrices (2D arrays) Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers An array is a multi-dimensional list  A matrix is a 2-dimensional array with rows and columns o E.g., the game Battleships Slide 5 of 14

6 MATLAB basics Variables and arrays – Matrices (2D arrays) Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers row 1 row 2 row 3 row 4 column 1column 2column 3column 4column 5 M ( row, column ) Answer: M( 2, 4 ) ? What is the address of the element in question? Answer: It is a 4 X 5 matrixWhat is the size of the matrix? Slide 6 of 14

7 MATLAB basics Variables and arrays – Vectors (1D arrays) Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers A vector is a one-dimensional array  It could be a row vector  Or, it could be a column vector A scalar is an array with only a single element  E.g., a 1X1 matrix or a vector with only one entry Slide 7 of 14

8 MATLAB basics Variables and arrays: Matrix, Vector, and scalar examples Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers Examples  ‘a’ is a 3  2 array or 3  2 matrix  ‘b’ is a 1  4 array or 4-element row vector  ‘c’ is a 3  1 array or 3-element column vector  ‘d’ is a scalar Slide 8 of 14

9 MATLAB basics Variables and arrays – Data types Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers MATLAB stores var as a double precision float Slide 9 of 14

10 MATLAB basics Variables and arrays - Initialization Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers In MATLAB variables are automatically created when they are initialized. >> x = [2.2 1.0 3.5] o Creates a 1  3 1D array ‘x’ (row vector) of doubles >> x = [2.2, 1.0, 3.5] o Same as above (the commas are optional) >> y = [2.2; 1.0; 3.5] o Creates a 3  1 column vector. >> y = [2.2 1.0 3.5] o Same as above Slide 10 of 14

11 MATLAB basics Variables and arrays: Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers Creating and initializing a matrix >> z = [ 1 2 3; 4 5 6] o Creates a 2  3 array (matrix) >> z = [ 1 2 3 4 5 6] o Same as above >> z = [ 1, 2, 3; 4, 5, 6] o Same as above >> z = [] o Creates an empty array (size is undefined) Must be a rectangular (or square) matrix All cols the same length and All rows the same length Must be a rectangular (or square) matrix All cols the same length and All rows the same length Slide 11 of 14

12 MATLAB basics Variables and arrays: Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers Abbreviated Initialization  X = start : increment : end >> x = 0.5 : 1.5 : 6 Initializing with built-in functions >> a = zeros(2, 5) >> b = ones(1, 4) >> c = zeros(3, 1) >> d = ones(3, 2) Can ask MATLAB to determine the size of an array >> size(d) Slide 12 of 14

13 MATLAB basics Variables and arrays: Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers Initializing with keyboard input  val = input('Any text to prompt user to enter a value '); o Try it!! MATLAB supports arrays with greater than two dimensions  We will rarely go beyond 2D arrays (i.e., matrices) How does MATLAB store matrices in memory?  Column major order o 1 st col, then 2 nd col, …. E.g., >> z = [ 1, 2, 3; 4, 5, 6] Answer: z(3) = z(1,2) = 2What is the value of z(3)? Slide 13 of 14

14 Next Lecture Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers Sub-Arrays Displaying data Slide 14 of 14


Download ppt "EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers."

Similar presentations


Ads by Google