Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function Name: filterByNumericField % Inputs (4): - (struct) A 1xN structure arr

ID: 3646251 • Letter: F

Question

Function Name: filterByNumericField
% Inputs (4): - (struct) A 1xN structure array
% - (char) The field name of a field containing numeric
% data
% - (double) A numeric value used to filter the structures
% - (char) A string representing which comparison to
% perform (either '>', '<', '<=', '>=', or '==')
% Outputs (2): - (struct) A structure array containing only structures
% that meet the criteria specified
% - (logical) A vector of logicals representing whether the
% information in each structure met the criteria
%
% Function Description:
% Write a function called "filterByNumericField" that filters the input
% structure array based on the field name, numeric value, and comparison
% operator specified. Your function should return a structure array that
% contains the structures whose data met the specfied criteria, as well
% as a vector of logicals representing whether or not the information in
% each position met the criteria. The vector should have a true in the
% positions where the criteria was met and a false otherwise. If none of
% the structures returns a true your output should be a 1x0 structure
% array containing the same fields as your input (see test case 2.)
%
% Notes:
% - The specified field will always exist in the input structure array
% - All the values for the given field across the structure array will be
% scalar doubles
% - The input string specifying the comparison to perform will always be
% either '>', '<', '<=', '>=', or '=='
% - The structure array is guaranteed not to be empty
% HINT: try out st(true,false) on a structure array named st in your command
% window
%
% Test Cases:
% s1 = struct('Class', {'CS 1371', 'CS 1372', 'CS 1331', 'CS 1301', ...
% 'CS 1315'}, 'Coolness', {9005, -42, 104, 11, 8536});
% [match1, log1] = filterByNumericField(s1, 'Coolness', 9000, '>');
% match1 => struct('Class', 'CS 1371', 'Coolness', 9005);
% log1 => [1 0 0 0 0]
%
% s2 = struct('Song', {'White & Nerdy', 'The Saga Begins', ...
% 'Your Horoscope for Today'}, 'Length', {2.50, 3.22, 4.01}, ...
% 'Track', {1, 1, 8});
% [match2, log2] = filterByNumericField(s2, 'Length', 3.00, '==');
% match2 => 1x0 struct array with fields:
% Song
% Length
% Track
% log2 => [0 0 0]
%
% s3 = struct('Numbers', {68, 42, 70, 17, 23, 164, 166, 113}, ...
% 'MoreNumbers', {3, 7, 29, 77, 127, 113, 78, 95}, ...
% 'EvenMoreNumbers', {173 14 100 19 167 1 135 142}, ...
% 'OkayThisIsGettingRidiculous', {1, 2, 3, 4, 5, 6, 7, 8});
%
% [match3, log3] = filterByNumericField(s3, 'EvenMoreNumbers', 100, '>=');
% match3 => struct('Numbers', {68, 70, 23, 166, 113}, 'MoreNumbers', ...
% {3, 29, 127, 78, 95}, 'EvenMoreNumbers', ...
% {173 100 167 135 142}, 'OkayThisIsGettingRidiculous', ...
% {1, 3, 5, 7, 8})
% log3 => [1 0 1 0 1 0 1 1]

Explanation / Answer

So much to read :(