Skip to main content

Section Homework 2

Before you begin ...

Permitted MATLAB Functions & Commands for Homework 2.

The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Operations:
round β€’ mod β€’ floor β€’ ceil β€’ sum
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_min

Returns both the minimum value in a list and the index location of the first minimum value without using min.
Inputs:
list (1 x N) double vector
Outputs:
min_val (1x1) double β€” minimum value in list
min_idx (1x1) integer β€” index of the first occurrence of min_val
Details:
β–Έ If there is more than one minimum, return the first one encountered.
β–Έ Use a loop to code this.
β–Έ Do not use MATLAB’s min function.
Examples:
[val,idx] = list_min([4 -3 1 9 7 -8 3])
% returns val = -8
%         idx = 6
[s,k] = list_min([4 0 1 9 0])
% returns s = 0
%         k = 2

Subsection list_max

Returns both the maximum value in a list and the index location of the first maximum value without using max.
Inputs:
list (1 x N) double vector
Outputs:
max_val (1x1) double β€” maximum value in list
max_idx (1x1) integer β€” index of the first occurrence of max_val
Details:
β–Έ If there is more than one maximum, return the first one encountered.
β–Έ Use a loop to code this.
β–Έ Do not use MATLAB’s max function.
Examples:
[V,k] = list_max([4 -3 1 9 7 -8 3])
% returns M = 9
%         i = 4
[M,x] = list_max([0 0 -4 0 -1 -9 0])
% returns M = 0
%         x = 1

Subsection list_avg

Computes the running average of a list of numbers, optionally continuing from a previous average and count.
Inputs:
list (1 x N) double vector β€” new data values to be averaged.
avg0 (1x1) double β€” previous average (optional).
n0 (1x1) integer β€” previous count of values averaged (optional).
Outputs:
avg (1x1) double β€” updated average after including list.
n (1x1) integer β€” updated total number of values used to find avg.
Details:
β–Έ Part of this exercise is to come up with a way to compute an updated average (avg) from a known previous average (avg0 & n0). Don’t ask me for the formula. Instead get out a pen and paper and figure it out.
β–Έ Both inputs, avg0 and n0, are optional. Meaning if they are not given, then your code should assume they are both zero and output the standard average.
β–Έ Your code can count the number of inputs that were used to call your function using the builtin nargin variable. If nargin is 1 then only 1 input was provided. In this case, it would mean that only list was given and you should set both avg0 & n0 to 0 by default.
β–Έ If for some reason two inputs were given, ignore the second input and use the default values above. You can assume that at least a list will be given and the number of inputs will not be more than 3.
Examples:
[a,n] = list_avg([4 0 1 9 0])
% returns a = 2.8000
%         n = 5
[a,n] = list_avg([4 -3 1 9 7 -8 3], -45)
% returns a = 1.8571
%         n = 7
[a,n] = list_avg([4 -3 1 9 7 -8 3], 2.8, 5)
% returns a = 2.2500
%         n = 12
[a,n] = list_avg([4 0 1 9 0 4 -3 1 9 7 -8 3])
% returns a = 2.2500
%         n = 12

Subsection list_merge_dups

Removes consecutive duplicate values from a list, returning a merged version.
Inputs:
list (1 x N) double vector β€” a list of numbers, possibly with consecutive duplicates.
Outputs:
merged_list (1 x M) double vector β€” list with consecutive duplicates removed, preserving order.
Details:
β–Έ Scan through list with a while loop. Whenever two consecutive elements are equal, delete one of them. Otherwise, move to the next index. Continue until the end of the list.
Examples:
merged_list = list_merge_dups([1 1 2 2 2 3 1 1 4])
% returns [1 2 3 1 4]
merged_list = list_merge_dups([5 5 5 5 5])
% returns 5
merged_list = list_merge_dups([7 8 9])
% returns [7 8 9]
merged_list = list_merge_dups([3])
% returns 3
merged_list = list_merge_dups([])
% returns []

Subsection list_value_counter

Counts the frequency of each unique value in a list.
Inputs:
list (1 x N) double vector β€” a list of numbers that may contain repeated values.
Outputs:
values (1 x M) double vector β€” the distinct values in list, order preserved by first appearance.
counts (1 x M) integer vector β€” the number of times each value occurs in list.
Details:
β–Έ Use a while loop to scan through the list. For each new value, count how many times it appears. Remove duplicates as you go so each value is counted only once.
Examples:
[values, counts] = list_value_counter([2 2 3 3 3 5 2])
% returns values = [2 3 5], counts = [3 3 1]
[values, counts] = list_value_counter([10 10 10 10])
% returns values = [10], counts = [4]
[values, counts] = list_value_counter([7 8 9])
% returns values = [7 8 9], counts = [1 1 1]

Subsection list_nearest_values

Finds the two values in a list that are closest to each other.
Inputs:
list (1 x N) double vector β€” a list of numbers.
Outputs:
nearest_values (1 x 2) double vector β€” the pair of values in list with the smallest absolute difference.
If list has fewer than two elements, return [].
Details:
β–Έ Scan all pairs of values in list and compute their absolute differences. Keep track of the pair with the smallest difference.
β–Έ Use nested while or for loops to code this.
β–Έ If more than one pair of numbers have the same distance, output the first pair found with this distance.
β–Έ Challenge: Can you write a program that only computes the distance between pairs once?
Examples:
nearest_values = list_nearest_values([10 4 22 7 5])
% returns [4 5]
nearest_values = list_nearest_values([100 101 250 500])
% returns [100 101]
nearest_values = list_nearest_values([4 -2 10 -1 -7 8])
% returns [-2 -1]
nearest_values = list_nearest_values([-3 0 -3 -3 3 1])
% returns [-3 -3]
nearest_values = list_nearest_values([42])
% returns []

Subsection list_sort

Given a list of numbers, this program returns the list in ascending order.
Inputs:
list (1 x N) double vector β€” row vector numeric values to be sorted.
Outputs:
list_sorted (1 x N) double vector β€” row vector containing the values in list in ascending order.
Details:
β–Έ To code this, initialize a vector of zeros to store the output. Use a loop that repeatedly (a) calls your list_min function, (b) adds the value to the output vector and (c) deletes the value from list
β–Έ Do not use MATLAB’s sort function.
Examples:
sorted = list_sort([-4 0 4 8 -12])
% returns: sorted = [-12  -4  0  4  8]
sorted = list_sort([8 1 3 4 9 1 -3])
% returns: sorted = [-3  1  1  3  4  8  9]

Subsection list_peak_locations

Returns the index location of all peaks in a list. A peak is any entry that is strictly greater than its immediate neighbors.
Inputs:
list (1 x N) double vector.
Outputs:
peaks (1 x M) double vector β€” the indices of all peaks in list. If there are no peaks, output [].
Details:
β–Έ Use a loop to code this.
β–Έ The first and last values in list cannot be peaks since they are missing a neighbor.
Examples:
peaks = list_peak_locations([1 3 -3 5 4.5 7 2])
% returns [2 4 6]
peaks = list_peak_locations([7 6 5])
% returns []  (no peaks)

Subsection list_smooth_values

Computes a moving average where each entry is replaced by the average of itself and up to \(k\) neighbors on each side (fewer near the ends).
Inputs:
list (1 x N) double vector.
k (1x1) nonnegative integer β€” number of neighbors on each side (optional, default \(1\)).
Outputs:
smoothlist (1 x N) double vector β€” smoothed values.
Details:
β–Έ To code this, use a loop to replace the current value with the average of k values to the left and k values to the right of the current value.
β–Έ Use your list_avg function to compute this average. The current value should be included in this average.
β–Έ If a value does not have k values to the left or right, then use as many values as are available.
β–Έ If k is omitted as an input, assume \(k=1\text{.}\) Use nargin for this.
Examples:
list_smooth_values([-4 0 4 8 -12])     % default k=1
% returns [-2 0 4 0 -2]
list_smooth_values([-4 0 4 8 -12], 2)
% returns [0 2.0000 -0.8000 0 0]
list_smooth_values([-4 0 4 8 -12], 4)
% returns [-0.8000 -0.8000 -0.8000 -0.8000 -0.8000]

Subsection list_properties

Computes a collection of descriptive properties of a list and returns them in a structure.
Inputs:
list (1 x N) double vector.
Outputs:
list_struct (1x1) struct containing the following fields:
  • min β€” minimum value in list
  • max β€” maximum value in list
  • range β€” difference between maximum and minimum
  • avg β€” average value
  • unique β€” distinct values
  • counts β€” frequency of each unique value
  • nearest_vals β€” pair of closest values
  • sorted β€” sorted copy of the list
  • peak_locations β€” indices of peaks in the list
  • smoothed2 β€” list smoothed with 2 neighbors
  • smoothed3 β€” list smoothed with 3 neighbors
Details:
β–Έ This function aggregates results from several helper functions (list_min, list_max, list_avg, list_value_counter, list_nearest_values, list_sort, list_peak_locations, and list_smooth_values).
β–Έ The output structure groups related properties into a single object for convenience.
Examples:
list_struct = list_properties([-2.4 1 -2 2 -7])

% returns a struct with fields:
%            min: -7
%            max: 2
%          range: 9
%            avg: -1.6800
%         unique: [-2.4000 1 -2 2 -7]
%         counts: [1 1 1 1 0]
%   nearest_vals: [-2.4000 -2]
%         sorted: [-7 -2.4000 -2 1 2]
% peak_locations: [2 4]
%      smoothed2: [-1.1333 -0.3500 -1.6800 -1.5000 -2.3333]
%      smoothed3: [-0.3500 -1.6800 -1.6800 -1.6800 -1.5000]
list_info = list_properties([-4 0 4 8 -12])

% returns a struct with fields:
%                min: -12
%                max: 8
%              range: 20
%                avg: -0.8000
%             unique: [-4 0 4 8 -12]
%             counts: [1 1 1 1 0]
%       nearest_vals: [-4 0]
%             sorted: [-12 -4 0 4 8]
%     peak_locations: 4
%          smoothed2: [0 2 -0.8000 0 0]
%          smoothed3: [2 -0.8000 -0.8000 -0.8000 0]
list_info = list_properties([3, 1, 7, 5, -9])

% returns a struct with fields:
%                min: -9
%                max: 7
%              range: 16
%                avg: 1.4000
%             unique: [3 1 7 5 -9]
%             counts: [1 1 1 1 0]
%       nearest_vals: [3 1]
%             sorted: [-9 1 3 5 7]
%     peak_locations: 3
%          smoothed2: [3.6667 4 1.4000 1 1]
%          smoothed3: [4 1.4000 1.4000 1.4000 1]