Vectorized Code, Logical Indexing

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
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.
Linear Equations in Linear Algebra
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Intro to Matrices Don’t be scared….
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Linear Algebra With Applications by Otto Bretscher. Page The Determinant of any diagonal nxn matrix is the product of its diagonal entries. True.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
Chapter 1 Number Sense See page 8 for the vocabulary and key concepts of this chapter.
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
Chapter 10 Review: Matrix Algebra
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Equation --- An equation is a mathematical statement that asserts the equality of twomathematicalstatement expressions. An equation involves an unknown,
Multi-Dimensional Arrays
LOGARITHMS,MATRICES and COMPLEX NUMBERS By Maurice Fritz and Karen Greer.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
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.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
Solving Equations. The equations are equivalent If they have the same solution(s)
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Sec 4.1 Matrices.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Introduction to Financial Modeling MGT 4850 Spring 2008 University of Lethbridge.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
13.3 Product of a Scalar and a Matrix.  In matrix algebra, a real number is often called a.  To multiply a matrix by a scalar, you multiply each entry.
Section 6.2 Solving Linear Equations Math in Our World.
Week 3 Day 1. Bring every assignment to next class for a progress report.
Matrices.
Matrices Introduction.
MTH108 Business Math I Lecture 20.
7.1 Matrices, Vectors: Addition and Scalar Multiplication
13.4 Product of Two Matrices
12-1 Organizing Data Using Matrices
Linear Algebra review (optional)
Mr. Hartzer, Hamtramck High School
CHAPTER 1.3 Solving Equations.
Other Kinds of Arrays Chapter 11
Matrix Operations SpringSemester 2017.
Section 7.4 Matrix Algebra.
WarmUp 2-3 on your calculator or on paper..
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
Learning Resource Services
Use of Mathematics using Technology (Maltlab)
Matlab tutorial course
Chapter 7: Matrices and Systems of Equations and Inequalities
Introduction to MATLAB [Vectors and Matrices] Lab 2
Logical Operations In Matlab.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Spreadsheets Objective 6.02
Announcements P3 due today
3.5 Perform Basic Matrix Operations
Linear Algebra review (optional)
Matlab Basics.
Spreadsheets Objective 6.02
Math review - scalars, vectors, and matrices
Matrix Operations SpringSemester 2017.
Announcements.
Announcements.
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Vectorized Code, Logical Indexing

Operations on Vectors and Matrices “Vectorized code”: using a function that can apply itself to each element of a vector or matrix. Most Matlab operators and functions can operate on vectors and matrices. E.g.: m = m *3; Works if m is a scalar Also works if m is a vector or matrix; each element of m is multiplied by 3.

Matlab functions support vectors/matrices -3 -2 -1 0 1 2 3 4 abs(v) m=round(rand(3,4)*10) sin(m)

Matrix vs. Arithmetic operations Multiplication, division, and power operators have special conventions when applied to matrices. They will typically do matrix operations rather than arithmetic operations. Matrix operations are topics of Linear Algebra. In this class, we work with arithmetic operations; so you need to make sure matrix operations are not used.

Matrix vs. Arithmetic operations When using an operator, Matlab gets to decide whether it will apply matrix vs. arithmetic operation, based on the size of the operands. Let A=[1 2; 3 4] and B=[10 20; 30 40] First Arg Operator Second Arg Operation Example Scalar * / ^ Arithmetic 5/3, 1/2, 5^2 Matrix * / A*2, A/2 * 2*A / 2/A ^ A^2 A*B, A/B

Element-wise operators You can force arithmetic operations on matrices by using “element-wise” operators. Place a period before the operator to make it element-wise. Whereas the meaning of X*Y depend on the size of X and Y, X.*Y is always arithmetic. To be safe, you may choose to always use element-wise operators.

Matrix vs. Element-wise operations >> B = [10 20; 30 40] 10 20 30 40 >> A=[1 2; 4 5] 1 2 4 5 Matrix multiplication >> A * B 70 100 190 280 Element-wise multiplication >> A .* B 10 40 120 200

Matrix vs. Element-wise operations 1 2 4 5 Matrix division >> 1 / A Error using / Matrix dimensions must agree. Element-wise division >> 1 ./A 1.0000 0.5000 0.2500 0.2000 Matrix power >> A ^ 2 9 12 24 33 Element-wise power >> A .^ 2 1 4 16 25

Exercise: Vectorized Code Let n be a positive integer. Calculate the sum: 1+ 1 2 2 + 1 3 3 +… 1 𝑛 𝑛

Logical Vectors >> vec = [ 1 5 8 9 2 7 ]; >> isg = vec > 4 >> doubleres = isg +5

Logical Indexing A logical vector/matrix can be used to index (select) elements of a vector/matrix. The logical vector/matrix must have the same size as the vector/matrix being indexed. >> vec = [ 1 5 8 9 2 7 ]; >> isg = vec > 4 >> vec (isg) >> vec (isg) = vec(isg) + 1

Exercise a = [ 5 2 1 7 9 6 8 ]; Create another vector b that contains the even elements of a. Change odd elements of a with 99. a = [ 5 2 1 7 9 6 8 ] Add 1 to odd elements of a. Remove all elements of a that are divisible by 3. Repeat steps above for a=[5 2 1; 7 9 6]

Logical Indexing of Matrices m=randi(100,3,5) m([1 3], [2 4]) m([5 8 10]) m([true false true], [false true false true false] ) m([true false true], [1 5] ) m([true false true false false true , true false true false false true false true false ])

Beware of 0/1. >> a = [ 5 2 1 7 9 6 ]; >> a ( [1 0 1 0 1 0] ) ??? Subscript indices must either be real positive integers or logicals. >> a( logical( [1 0 1 0 1 0] ) ) >> a( [true false true false true false] )

find find(x) function returns the indices of non-zero elements of x. >> find ( [ false true false true ] ) >> find ( [ 3 0 2 5 0 ] ) >> vec = [ 1 5 8 9 2 7 ]; >> find ( vec > 5 )

Equivalence of logical indexing & indexing with find() find() can be used to convert a logical index to a numeric index Assuming I is a logical vector/matrix, the following expressions are equivalent: m ( I ) m ( find(I) )

Exercise: Zero-crossing Find the indices of the elements of vector v, that are followed by a number with an opposite sign. Hints: sign, diff, find v = [ 5 -1 -2 -1 3 8 -2 ]

Logical Operators: Element-wise and, or, not >> a = [ 3 0 5 1 ]; >> b = [ 0 0 -5 0 ]; >> a & b >> a | b >> ~a

Programming Concept When you want to operate on elements of a matrix that satisfy a condition, first construct an Index for elements that satisfy the condition.

Exercise Let A=magic(4) Replace with 0, all elements of A that are odd and whose log() is greater than 2.

Logical Functions: all(), any() >> vec1 = [1 3 1 1 2]; >> all ( vec1 ) >> any ( vec1 ) >> vec2 = [ 1 1 0 1] >> all ( vec2 ) >> any ( vec2 )

any(), all() with matrices If a matrix is given as input, these functions apply to each column. >> A=[0 0 3 2; 4 0 0 5; 6 0 5 8] 0 0 3 2 4 0 0 5 6 0 5 8 >> any( A ) 1×4 logical array 1 0 1 1 If you want to apply to each column, provide the second “dimension” argument. >> any(A, 2) 3×1 logical array 1 any(A,1) is same as any(A)

meshgrid meshgrid creates a mesh (all combinations) of coordinates [x,y] = meshgrid([1 2 3], [5 6]) x = 1 2 3 y = 5 5 5 6 6 6

meshgrid example Find all integers x, y less than 20 where 𝑥 2 + 𝑦 = 3∗𝑥

meshgrid example Make a 3D plot of the following function: 𝑓 𝑥,𝑦 =𝑥∗sin⁡(𝑥+2∗𝑦) Use x=0:.1:5, y=0:.1:5 Use plot3 and surf functions.