Question: Please create a function that returns the minimum element and its index in a numeric array ( single - row vector or single - column

Please create a function that returns the minimum element and its index in a numeric array (single-row vector or single-column vector). Name your function as findMin(). The input to the function can be either a row vector, a column vector, or a scalar (of numeric data types), otherwise the function returns a vector of 2NaNs ([NaN NaN]). Your function returns 2 scalar values in the format of [min_val, min_idx] which represents the value and index of the minimum element, respectively. For example, return [[[2,1]] if input is the scalar 2. If the array contains duplicates of the minimum element, keep only the smallest index of the min_val. DO NOT return multiple indices. You are not permitted to use| the built-in min() or max() functions. You can exploit if, for or while structures to create your functions. Any algorithm or method can be used. The function header is already givfunction [min_val, min_idx]= findMin(input)
if isscalar(input)
min_val = input;
min_idx =1;
return;
end
% Check if input is a vector
if isvector (input)
min_val = NaN;
min_idx = NaN;
return;
end
% Initialize min_val and min_idx
min_val = input(1);
min_idx =1;
% Iterate over the input vector
for i=2 : length(input)
if input(i) min_val
min_val = input(i);
min_idx =i;
end
end
end
Code to call your function ?
1[min_val, min_idx]= findMin ([43321])
Assessment: 3 of 4 Tests Passed (80%)
Correct output for a row vector.
Correct output for a scalar.
[ NaNNaN ] if input is not a numeric array or is of incorrect sizesen as function [min_val, min_() idx]= findMin (input). Function ?
 Please create a function that returns the minimum element and its

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

It looks like you need to ensure the function correctly handles nonnumeric inputs and both row and c... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!