Skip to main content

Section Homework 1

Before you begin ...

Permitted MATLAB Functions & Commands for Homework 1.

The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Operations:
round โ€ข mod โ€ข floor โ€ข ceil
Size/Dimensions:
length โ€ข numel โ€ข size โ€ข height โ€ข width
Creation:
zeros โ€ข ones โ€ข true โ€ข false
Logical:
all โ€ข any
Flow Control
Conditional:
if โ€ข switch-case โ€ข try-catch
Loops:
for โ€ข while โ€ข continue โ€ข break โ€ข return
Strings and Character Arrays
Operations:
join โ€ข replace
Conversion:
num2str โ€ข str2num โ€ข str2double โ€ข string โ€ข char
Other
Printing Text:
fprintf โ€ข disp โ€ข display โ€ข input โ€ข assert
Random Generators:
rand โ€ข randi โ€ข rng
Special Variables:
nargin
Type Detection:
isnan โ€ข isfield โ€ข isempty
Points will be deducted from any programs using functions outside this list.

Subsection list_sum

Returns the sum of all the numbers in a list.
Inputs:
list (1 x N) double vector.
Outputs:
total_sum (1x1) double โ€” sum of the values in list
Details:
โ–ธ Use numel, size, or length to get the number of values in the list.
โ–ธ Use a for or while loop to code this.
โ–ธ Do NOT use MATLABโ€™s sum command.
Examples:
S = list_sum([2 1 3 -1])
% should return S = 5
S = list_sum([-2 1 0 -8 10 9 0 3.22 9 -1 -100 9.54])
% should return S = -69.2400

Subsection list_is_nonnegative

Returns true if a list contains no negative numbers. The list may have any length.
Inputs:
list (1 x N) โ€” list of numeric values (doubles)
Outputs:
result (1x1) โ€” true if list has no negative numbers
Details:
โ–ธ Use numel, size, or length to get the number of values in the list.
โ–ธ Use a for or while loop together with an if-statement.
โ–ธ No other built-in MATLAB commands are allowed.
โ–ธ Zero is a non-negative number.
Examples:
result = list_is_nonnegative([111 -90 20 66 -167 42])
% returns false (0)
result = list_is_nonnegative([1 1 1 0 1 2 3 9 7 5 0 9 9 4])
% returns true  (1)

Subsection list_occurrences

Returns the number of times a number occurs in a given list of numbers.
Inputs:
list (1 x N) double vector
num (1x1) double โ€” number we are looking for in list
Outputs:
num_count (1x1) integer โ€” number of times num occurs in list
Details:
โ–ธ Use numel, size, or length to get the number of values in the list.
โ–ธ Use a for or while loop together with an if-statement.
โ–ธ No other built-in MATLAB commands are allowed.
Examples:
num_count = list_occurrences([1 1 1 -9 0 0 6 6 -1 6 7 1 2], 1)
% returns 4
num_count = list_occurrences([1 1 1 -9 0 0 6 6 -1 6 7 1 2], -6)
% returns 0

Subsection list_sign_change_count

Returns the number of times consecutive numbers have a different sign in the list.
Inputs:
list (1 x N) double vector
Outputs:
count (1x1) integer โ€” number of sign changes in list
Details:
โ–ธ Use length to get the number of values in the list.
โ–ธ Use a for or while loop together with an if-statement.
โ–ธ Changing from a positive or negative to a zero does not count as a change of sign.
โ–ธ Only count changes between adjacent numbers.
Examples:
count = list_sign_change_count([1 -1 1 -9 6 6 -1 -2 4])
% returns 6
count = list_sign_change_count([1 1 1 -9 0 0 6 6 -1 6 7 1 2])
% returns 3

Subsection get_direction

Returns an objectโ€™s direction given two 2D positions \((x,y)\text{:}\) the initial position position_old and the final position position_new. The direction must be one of the eight cardinal directions:
'N', 'S', 'E', 'W', 'NE', 'NW', 'SE', 'SW'.
Inputs:
position_old (1 x 2) double vector โ€” contains the (x, y)-coordinates of the initial position
position_new (1 x 2) double vector โ€” contains the (x, y)-coordinates of the final position
Outputs:
direction (1x1) or (1 x 2) character vector โ€” indicating the movement direction
Details:
โ–ธ If position_old equals position_new, return the empty string ''.
โ–ธ Any movement in a primary direction should be reflected (for example, moving 10 units north and only 0.1 units west should return 'NW')
โ–ธ Use an if-statement.
Examples:
direction = get_direction([2 1], [3 1])
% returns 'E'
direction = get_direction([-2 1], [0 -8])
% returns 'SE'
direction = get_direction([8 -2], [-2 5])
% returns 'NW'
direction = get_direction([0 -2], [0 -2])
% returns ''