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

Write a function with the following header: function [minimum, index] = my_minim

ID: 3794036 • Letter: W

Question

Write a function with the following header: function [minimum, index] = my_minimum_index(vector) where: vector is a row vector of class double that contains at least one value. minimum is a scalar of class double that represents the minimum value of vector. index is a scalar of class double that represents the index of the first occurrence in vector of the minimum value of vector. If all the values in vector are NaN, then minimum should have the value NaN and index should have the value 1. In any other case, NaN should never be considered the minimum value of vector. You may not use Matlab's built-in functions min, max, find, and sort in this question. Test cases: >> [minimum, index] = my_minimum_index(5) minimum = 5 index = 1

Explanation / Answer

% Approach First Remove all the NaN and create another array vector1

% After removing nan if vector is empty that means all values are nan then
% we return nan and 1

% If there is any element after removing nan then we find the minimum among
% remain elements

% Once we get minimum element we will check the original location of the
% element and return the minimum and index
function [minimum,index] = my_minimum_index(vector)
clc; % Clear Screen
k=1; % Temp variable for new vector creation
vector1=[]; % new Empty Vector
for i=1:numel(vector)
if(~isnan(vector(i))) % Check for Not a Nan if yes then store in new array
vector1(k)=vector(i);
k=k+1;
end
end
if(numel(vector1)==0 && numel(vector)~=0) % Check that new array is empty or not if yes then return nan and 1
minimum=NaN;
index=1;
return;
end
minimum=vector1(1); % assign first element of new vector into minumum and then compare this element with other elements
for i=2:numel(vector1)
if(minimum>vector1(i))
minimum=vector1(i); % minimum element logic
end
end
for i=1:numel(vector)
if(vector(i)==minimum) % Check the original Index of the Element
index=i;   
end
end
end

%% Test Cases in Matlab

>> [minimum,index]=my_minimum_index([nan 6 1 7 -2 nan ])

minimum =

-2


index =

5

>> [minimum,index]=my_minimum_index([nan nan nan nan nan])

minimum =

NaN


index =

1

>> [minimum,index]=my_minimum_index([5])

minimum =

5


index =

1

>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote