Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers"— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers
MATLAB Basics 1: Variables & Arrays Lecture 4 EGR 115 Introduction to Computing for Engineers

2 EGR 115 Introduction to Computing for Engineers
Lecture Outline MATLAB Basics Variables and arrays Lecture 4 EGR 115 Introduction to Computing for Engineers

3 MATLAB basics Variables and arrays:
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 E.g., >> accel_due_to_gravity = 9.81; % g in m/s^2 Variables exhibit a variety of properties Scope or visibility Is it globally or only locally visible? Extent Life-time of the variable? Type Is it an integer, float, character, … Lecture 4 EGR 115 Introduction to Computing for Engineers

4 MATLAB basics Variables and arrays:
Strongly typed languages Requires that the data type of the variable must be declared before it is used 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 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 Lecture 4 EGR 115 Introduction to Computing for Engineers

5 MATLAB basics Variables and arrays – Matrices (2D arrays)
An array is a multi-dimensional list A matrix is a 2-dimensional array with rows and columns E.g., the game Sudoku Lecture 4 EGR 115 Introduction to Computing for Engineers

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

7 MATLAB basics Variables and arrays – Vectors (1D arrays)
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 Lecture 4 EGR 115 Introduction to Computing for Engineers

8 EGR 115 Introduction to Computing for Engineers
MATLAB basics Variables and arrays: Matrix, Vector, and scalar examples 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 (or 11 array) Lecture 4 EGR 115 Introduction to Computing for Engineers

9 MATLAB basics Variables and arrays – Data types
MATLAB data types: The default data type in MATLAB is double i.e., 64-bit floating point type number E.g., >> var = 22; Imaginary numbers are distinguished by ‘i’, or ‘j’ E.g., >> var = 11.2i; Variables of type character (requires single quotes) E.g., >> comment = ‘Please Help Me’; Actually, a 114 character array!! MATLAB stores var as a double precision float Lecture 4 EGR 115 Introduction to Computing for Engineers

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

11 MATLAB basics Variables and arrays:
Creating and initializing a matrix >> z = [ 1 2 3; 4 5 6] Creates a 23 array (matrix) >> z = [ ] Same as above (easier to read?) >> z = [ 1, 2, 3; , 5, 6] Same as above >> z = [] Creates an empty array (size is undefined) Matrix must be a rectangular (or square) All cols the same length and All rows the same length Lecture 4 EGR 115 Introduction to Computing for Engineers

12 MATLAB basics Variables and arrays:
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) Lecture 4 EGR 115 Introduction to Computing for Engineers

13 MATLAB basics Variables and arrays:
Initializing with keyboard input val = input('Any text to prompt user to enter a value '); 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 (some Prog languages are row major) All of 1st col, then all of 2nd col, …. E.g., >> z = [ 1, 2, 3; , 5, 6] e.g., C and C++ are row major!! What is the value of z(3)? Answer: z(3) = z(1,2) = 2 Lecture 4 EGR 115 Introduction to Computing for Engineers

14 EGR 115 Introduction to Computing for Engineers
Next Lecture Sub-Arrays Displaying data Lecture 4 EGR 115 Introduction to Computing for Engineers


Download ppt "EGR 115 Introduction to Computing for Engineers"

Similar presentations


Ads by Google