The following built-in MATLAB functions and commands are permitted for this assignment.
Vector/Matrix
Flow Control
Strings and Character Arrays
Other
Points will be deducted from any programs using functions outside this list.
AI for any part of this problem.function_name.m, where each function_name is listed below.list_min
min. | Inputs: | list (1 x N) double vector
|
| Outputs: | |
| 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.
|
[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
list_max
max. | Inputs: | list (1 x N) double vector
|
| Outputs: | |
| 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.
|
[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
list_avg
| 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: | |
| 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.
|
[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
list_merge_dups
| 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.
|
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 []
list_value_counter
| 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.
|
| 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.
|
[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]
list_nearest_values
| 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.
|
| Details: | βΈ Scan all pairs of values in list and compute their absolute differences. Keep track of the pair with the smallest difference.
|
βΈ 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?
|
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 []
list_sort
| 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.
|
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]
list_peak_locations
| 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.
|
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)
list_smooth_values
| Inputs: | list (1 x N) double vector.
|
| 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.
|
|
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]
list_properties
| Inputs: | list (1 x N) double vector.
|
| Outputs: | list_struct (1x1) struct containing the following fields:
|
| 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.
|
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]