CSE Midterm Study Sheet
Transcript of CSE Midterm Study Sheet
-
8/11/2019 CSE Midterm Study Sheet
1/3
Numbers
Logicals
Assignment Statement- assigning a value to a variable. Ex. A = 5
If Statementsifconsequent statementsend
If Else If Statements- these are like the if else statements except it has multiple
conditions ex. if temp>100 disp(a) elseif temp>90 disp(b) elseif temp>80 disp(c)
else disp(d)Relational Operators< > = == (less than, greater than, less than/equal to,
greater than/equal to, the equal sign with the tilde in front just means not equal to,
while the double equal sign means equal to)
Logical Operators- && means and, double vertical line means or, the tilde means
not)
Pi3.14
Sequences/Colon Notation1:5 (values 1-5) or 28:-9:1(start at 28, count by -9,
dont go below 1)Vectora linear group of numbers arranged vertically or horizontally. Ex. [0 0 0
0]
Vector Masking(see example at bottom of page)
Boolean Valueseither true or false
Stringa sequence of characters
For Loopseasier choice when you know how many times you want to repeat a
set of instructions (see format at bottom of page)
While Loops- runs the body repeatedly as long as the condition is true, or until thecondition is false. It tests the condition before doing the body for the first time. If
the condition is false right away, the body will not be run at all (see format at
bottom of page)
User-Defined Functions- .m files that the user creates (did many in lab and HW)
Built In Functions:
sumcomputes the sum of the elements in a vector
remremainder after division ex. rem(4,2)0 rem(8,3)2
minmin(x) is the smallest element in x
maxmax(x) is the largest element in xfloor(x) rounds elements of x to the nearest integers towardsneg. infinity.
ceilceil(x) rounds elements of x to nearest integers towards pos. infinity
linspacecalculates increments between values ex. linspace(1,10,3)1.0000
5.5000 10.0000
randgenerates one or more uniformly distributed random numbers(start
w/#s b/w 0 and 1 but can be manipulatedby multiplying) ex. rand(1, 4)0.1270
0.9134 0.6324 0.0975
-
8/11/2019 CSE Midterm Study Sheet
2/3
sqrttakes the square root of a value
randperm- p = randperm(n) returns a row vector containing a random
permutation of the integers from 1 to n inclusive. Ex. randperm(6)[3 2 6 4 1
5]
strcmpcompares two strings for equality
lengthdetermines the largest dimension of the matrix x
size gives you the number of rows and columns of a variable ex. [ 1 2 3]would be 1 3
findtells you where a vector is true. Ex. find([1 0 1 0)] 1 3
dispdisplays the content of a variable
zeros- will give you all zeros depending on what dimensions are given ex.
zeros(2,3) will give a 2x3 vector
oneswill give you all ones depending on dimensions given
doublechanges x from a symbolic variable to a matrix variable
charconverts input into to a stringclassreturns a string specifying the class of the object
num2str- converts numbers to text strings ex. num2str(pi) = 3.1416
str2numconverts strings to numbers
*Array and Matrix are the same thing, everything in MatLab is a matrix
*Ctrl + C to stop a never ending loop!
*Two phases to a functions life: 1. When you define the function 2. When you call
the function*vector masking ex.V = [10 20 30 40 50]; explanation:[10 20 30 40 50]
M = [true false true true]; [true false true true] X
V(M)
ans =
10 30 40
*while loop setup while condition
body
end
*for loop setup for variable = vectorbody
end
*function setup function [ output_args ] = Title( input_args )
(body)
end
-
8/11/2019 CSE Midterm Study Sheet
3/3
Input Function- NUM = input(PROMPT) displays the PROMPT string on the
screen, waits for input from the keyboard, evaluates any expressions in the input,
and returns the value in NUM.
fprintfdisplays something in a specific format(%g, %s, etc.)
switch statement- The general form of the switch statement is:
switch switch_expr
CASE case_expr,statement, ..., statement
CASE {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
OTHERWISE,
statement, ..., statement
END
Cell array- cell(N) is an N-by-N cell array of empty matrices.cell(M,N) or cell([M,N]) is an M-by-N cell array of empty
matrices.
cell(M,N,P,...) or cell([M N P ...]) is an M-by-N-by-P-by-...
cell array of empty matrices.
Structure array- S = struct('field1',VALUES1,'field2',VALUES2,...) creates a
structure array with the specified fields and values. The value
arrays VALUES1, VALUES2, etc. must be cell arrays of the same
size, scalar cells or single values. struct([]) creates an empty structure.More Functions:fopen- opens the file FILENAME for read access.
FILENAME is a string containing the name of the file to be opened.
fgetlRead line from file, discard newline character.
fcloseclose the file
feof - Test for end-of-file.
ST = feof(FID) returns 1 if the end-of-file indicator for the
file with file identifier FID has been set, and 0 otherwise.
The end-of-file indicator is set when a read operation on the fileassociated with the FID attempts to read past the end of the file.