Sunteți pe pagina 1din 4

TEST 1 REVIEW GUIDE

To best prepare for the exam, do as many practice exams as you can. This review guide is not a
completely exhaustive list.

Topics:
• Function Headers and Conceptual Questions
• Basic Function Operations
• Vectors/Strings and their functions
• Numerical Indexing
• Logical Indexing/Masking
• Arrays

Function Headers:
Examples of Valid Function Headers
function out = myfunc(in)
function [out] = functionName(in)
function [a b] = func(in, in2)
function name(in)
function name()
function name
function a = name()
function [] = name()

Examples of Invalid Function Headers


Function out = myfunc(in)
function out1 out 2 = func(in) %multiple outputs need brackets
function out = func(in in2) %multiple inputs must have commas
function = name(in) %no assignment operator unless an output is defined

Be able to define/describe:
• Function Scope
• Encapsulation
• Abstraction
• Hard Coding
• Helper Functions
• Dimension Mismatch
• Indexing Out of Bounds
• Function Operations:

Functions to be familiar with


mod() >> know how to use it to find numbers divisible by 2 or 3 or any other
number
[a, b] = sort() >> first output is sorted vector, second output is positions

char() vs. num2str() >> char converts a double into its corresponding
character letter ( 65 à ‘A’) while num2str coverts a double into the
character of that number (65 à ‘65’)

double() vs str2num() >> double converts a char into its corresponding


ASCII value( ‘A’ à 65 ) while str2num coverts a string of a number into the
double version of that number (‘65’ à 65)

[word, rest] = strtok(str, delimiter)>> keep in mind that the


second output will start with the delimiter
Ex. [word, rest] = strtok(‘Good Luck!’
word = ‘Good’
rest = ‘ Luck!’ ßbegins with a space

Vectors/Strings
Indexing
Numerical Indexing: simply say which index of the vector/ string you want to
access
“Store the third value of vec in a variable named a”
a = vec(3)
“Store the first and third values of vec in a variable named b”
b = vec([1,3]) ß multiple indices must be kept in brackets
vec(1,3) is wrong
“Store every other value of vec in a variable named c”
c = vec(1:2:end)

Creating Vectors
Manual Entry: [1 3 5 7]
Colon Operator: 1:2:7
Using different functions: linspace(starting num, ending numb, steps)
ones(rows, columns)
zeros(rows, columns)

Logical Indexing
Creating and using a mask
“Find all the values greater than 10 in vec”
mask = vec > 10
“Find all the values greater than 10 in vec and replace them with 0”
mask = vec > 10
vec(mask) = 20
“Find all the values greater than 10 in vec and add 5 to those values only”
mask = vec > 10
vec(mask) = vec(mask) + 5
“Find all the values in vec divisible by 10 and store these values in a”
mask = mod(vec,10) == 0
a = vec(mask)

Functions involving logical values


all() à if every value is true, will return true, otherwise will return false
vec = [true true true true]
a = all(vec)
a = true

vec2 = [true false true true]


b = all(vec)
b = false

any() à if any value is true, will return true, otherwise will return false
vec = [false true false false]
a = any(vec)
a = true

vec2 = [false false false false]


b = any(vec)
b = false

How Masking Works


vec = [3 5 1 2 6]
mask = [true false true true false]
a = vec(mask)
a = [3 1 2]
b = vec(~mask) ß the tilde (~) flips the mask (trues become false and vice
versa)
b = [5 6]

Arrays
Indexing
Must always, at least for this test, index arrays by using two values, rows and
columns

arr is a 4x4 array


a = arr(4) ß for this test, you should never be indexing arrays like this
a = arr(3,4) ß finds the value in the third row, fourth column

b = arr(:,3) ß finds values from all the rows, third column (aka, accesses
the third column)
c = arr(2, :) ß finds values in second row, all the columns (aka 2nd row)

Sorting arrays.
“Sort the array based off of the values in the first row in descending order”
[~ , ind] = sort(arr(1,:), ‘descend’);
arr = arr(:, ind); ß When you rearrange an array based off of values
in a specific row, the indices you find from sorting
are used to rearrange the columns. The order of
the rows doesn’t change.

“Sort the array based off of the values in the third column in ascending order”
[~ , ind] = sort(arr(:,3));
arr = arr(ind, :); ß When you rearrange an array based off of values
in a specific column, the indices you find from sorting
are used to rearrange the rows. The order of the columns
doesn’t change.

S-ar putea să vă placă și