Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.

Slides:



Advertisements
Similar presentations
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Advertisements

EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Lecture 3 Creating M-files Programming tools: Input/output(assign/graph-&-display) Repetition (for) Decision (if) © 2007 Daniel Valentine. All rights reserved.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
JavaScript, Fourth Edition
JavaScript, Third Edition
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
Matrix Mathematics in MATLAB and Excel
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
MATLAB Fundamentals.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Spreadsheets Objective 6.02
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
2440: 211 Interactive Web Programming Expressions & Operators.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Input, Output, and Processing
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Working with Arrays in MATLAB
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 JavaScript.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
An Introduction to Programming in Matlab Emily Blumenthal
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
ECE 1304 Introduction to Electrical and Computer Engineering
ECE 1304 Introduction to Electrical and Computer Engineering
Manipulating MATLAB Matrices Chapter 4
BASIC ELEMENTS OF A COMPUTER PROGRAM
© 2016 Pearson Education, Ltd. All rights reserved.
JavaScript: Functions.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Vectors and Matrices I.
Communication and Coding Theory Lab(CS491)
Arrays and Matrices in MATLAB
Array Creation ENGR 1181 MATLAB 02.
INTRODUCTION TO MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Matlab Training Session 2: Matrix Operations and Relational Operators
Matrices in MATLAB Dr. Risanuri Hidayat.
EECS Introduction to Computing for the Physical Sciences
Presentation transcript:

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.

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.

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)

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)

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.

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

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

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:

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: Columns Rows

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) Columns Rows ans =

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

Hands-on Enter (input) the following matrix into MATLAB: Enter (input) the following matrix into MATLAB: whiteRabbit =

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

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.^

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.^

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

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 = c = 2+3^2+1/(1+2)4 th c = c = 2+3^2+1/(1+2)5 th c =

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 = b =

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.

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.

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 = B =

D = 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 =

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 = F =

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 =

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 = A =

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 = I =

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 = ans =

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 = ans = G =

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 = ans = 1 3 7

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.

Hands-on exercise: Create the following matrix using colon notation: Create the following matrix using colon notation: W = 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:

Hands-on continued Create the following matrix using colon notation: Create the following matrix using colon notation: X = 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,:)

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.

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.