Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vectorized Code, Logical Indexing

Similar presentations


Presentation on theme: "Vectorized Code, Logical Indexing"— Presentation transcript:

1 Vectorized Code, Logical Indexing

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

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

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

5 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

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

7 Matrix vs. Element-wise operations
>> B = [10 20; 30 40] >> A=[1 2; 4 5] Matrix multiplication >> A * B Element-wise multiplication >> A .* B

8 Matrix vs. Element-wise operations
Matrix division >> 1 / A Error using / Matrix dimensions must agree. Element-wise division >> 1 ./A Matrix power >> A ^ 2 Element-wise power >> A .^ 2

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

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

11 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 = [ ]; >> isg = vec > 4 >> vec (isg) >> vec (isg) = vec(isg) + 1

12 Exercise a = [ ]; Create another vector b that contains the even elements of a. Change odd elements of a with 99. a = [ ] 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]

13 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 ])

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

15 find find(x) function returns the indices of non-zero elements of x.
>> find ( [ false true false true ] ) >> find ( [ ] ) >> vec = [ ]; >> find ( vec > 5 )

16 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) )

17 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 = [ ]

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

19

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

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

22 Logical Functions: all(), any()
>> vec1 = [ ]; >> all ( vec1 ) >> any ( vec1 ) >> vec2 = [ ] >> all ( vec2 ) >> any ( vec2 )

23 any(), all() with matrices
If a matrix is given as input, these functions apply to each column. >> A=[ ; ; ] >> any( A ) 1×4 logical array 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)

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

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

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


Download ppt "Vectorized Code, Logical Indexing"

Similar presentations


Ads by Google