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 ''