{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS Class Notes.docx.

# CS Class Notes.docx. - CS1371 Notes Functions Defining...

This preview shows pages 1–4. Sign up to view the full content.

CS1371 Notes Functions Defining functions Must start with “function” Can have any number of outputs If >1 outputs, they must be contained in square brackets [ ] The assignment operator (=) : only put it if you have outputs a function CAN have outputs or inputs Function name o letters, numbers, underscores Inputs: o can have any number o If >0, contain in parentheses and comma-separated o Function header Function [quotient] = myDivide(in1, in2) Don’t put semi-colon after function header Quotient and myDivide cannot be the same Quotient = in1 ./ in2; End Formal parameters: the names in the f × n header that the function will know the values as Actual parameters: the values that you pass into the function e.g. Out = myDivide (5000,3) mod (x,y) gives the remainder when x is divided by y Vectors = zero or more numbers collected together Numbers are elements of the vector Elements have 2 properties: value, position Vectors store only one type of data at a time (logical OR double, not BOTH) RowVec = [3 7 2 9] ColVec you put it in like this in MATLAB: [3; 7; 2; 9] or [3 7 2 9]’ Operators: Colon operator: o Takes in a starting number o Vec = start: step: end (step : increment) o If step size isn’t specified, default is 1. o b = 5:-1:2 [5 4 3 2 1] o c = 5:2 [ ] empty o d = 5:-1:7 [ ]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Linspace: o Linspace (start, end, amount of evenly spaced numbers) o Displays evenly spread numbers between start and end, with the numbers increments I specified. o If the number of increments is not specified, default number would be 100 Zeros o Zeros (rows, columns) Concatenating vectors: Putting vectors together E.g.: o Vec = [27 32 53 47] o Vec2 = [1 2 3 4 5] o Vec3 = [Vec2, Vec] o Vec3 = [1 2 3 4 5 27 32 53 47] If you try to concatenate vectors that are different in terms of dimensions, an error would occur… OBVIOUSLY. Vec4 = [2 4 6]; Vec5 = [1 2 3] Vec6 = [Vec4 + Vec5] [3 6 9], adds element by element. You can’t use “sum” because then that would add all the elements up together, not separately. If you use “*” or “/” without the “.”s, MATLAB will try to perform matrix multiplication or division. Vectors have to have the same length and shape in order to perform algebraic operations. c = [5,6,7]+3 [8,9,10] Length () z = linspace (1, 2) x = length (z) x = 100 because default for linspace is 100 y = [3,4,5,6] y = length(y) 4 x = length(2:1) 0 vec = [9 11 23 4] vec(7) = vec(1) vec = 9 11 23 4 0 0 9] length(vec) = 7 Accessing / Indexing vectors vec ( ) = … - changes value of elements at specified positions
… = vec ( ) - grabs specified elements and store them in variable Vec2 = [3 5 2 7] I want to get out whatever value is in position three Num = Vec2 (3) 2 Can’t index with fractions or negative numbers.

This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

### Page1 / 19

CS Class Notes.docx. - CS1371 Notes Functions Defining...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online