{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exam 3 Review

# For example if you have an array x 1 7 4 1 13 x3 is

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ay x = [ 1 7 4 1 13 ] x(3) is 4 x([1 3 5]) is [1 4 13] Using Subarrays In addition you can use : notation to select elements. end denotes the last element. a = [ 1 5 8 10 3 21 9 ] b = a(1:2:end) b=[1839] A colon on its own selects all values x = [ 7 8 9 ; 1 2 3; -1 -2 -3 ] y = x( :, 1:2 ) You can also assign to subarrays y= 7 1 -1 8 2 -2 Vectorizing It is possible to use for-loops to perform formathematical operations on arrays. But taking advantage of MATLAB's natural ability to work with arrays and vectors greatly speeds up code. These expressions are almost always faster than for or while loops Vectorizing Expressions When we create a matrix by using a logical operator that array is a logical array x = [ 1 7 10; 9 4 2 ]; y = x > 5; We can also make any array logical by using the logical function z = logical(x); 9 Vectorizing Expressions A logical array can be used to determine when an operation will be carried out (matrix addressing). Suppose we wanted to subtract 2 from all values greater than 5 x = [ 1 7 10; 9 4 2 ]; y = x > 5; x= 1 5 8 7 10 x(y) = x(y)-2; x(y)9 742 y= 011 100 Outline Lecture 15 and 16 Intro to MATLAB and Matrices Lecture 17 Script and Function Files Lecture 18 and 19 MATLAB Programming Lecture 20 Subarrays and Vectorization Lecture 21 Plotting in MATLAB Plotting Data in MATLAB To plot data use the function plot(x, y) x is a vector of x values y is a vector of y values x = 1:0.1:10; y = x .^ 2 10 .* x + 15; plot(x, y)...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online