Question: Write a function named largest that will receive one vector vec as input argument. The elements of vec are of type double, and they can
Write a function named largest that will receive one vector vec as input argument. The elements of vec are of type double, and they can be positive or negative numbers. The function will return the largest element of vec that is adjacent to an element with the value 0(zero). You are required to use the programming method (loops, conditional statements). You are NOT allowed to use the following built-in Matlab functions: imdilate, unique, diff . You are allowed to use the 'find' built-in Matlab function. The function should return the desired output regardless of the input vector size.Here is the function but the function does not work well if the adjacent numbers to 0 are both negative value, the answer will be 0 which is not true.
function [maxi] = largest(vec)
index = 1;
maxi = 0
for num = vec if(index > 1 && index+1 <= length(vec) && (vec(index-1) == 0 || vec(index+1) == 0))
if(num > maxi) maxi = num
end
end
index = index+1
end
fprintf('Largest number found : %d ',maxi)
end % end of largest
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
