In: Computer Science
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
%% code works in both matlab and octave
%%save below function as num_local_max.m
function nlocalmax=num_local_max(x)
nlocalmax=0;
for i=2:(numel(x)-1)
%%condition for local maximum
if((x(i-1)<x(i)) && (x(i)>x(i+1)))
nlocalmax=nlocalmax+1;
end
end
end
%%end of function
%%run below code in console
%since data is not given creating the random 1000 numbers
%%generate random numbers between 0.0044 and 0.67
nums = (0.67-0.0044)*rand(1,1000) + 0.0044;
num_local_max(nums)