MATLAB: Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise.

indexingMATLAB

Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise. this is my code
function tf = mono_increase(x)
% we need to check every number
L=length(x);
% we nust use for loop
% then we must use if statement
if L==1
tf=true; % adding a condition for single element
return %we must close the case for faster operation
end
for ii = 1:(L-1)
if x(ii+1) > x(ii) % we have problems in the -ve numbers
tf= true ;
else
tf=false ;
end
end
it doesn't work for negative numbers like : input x= [ -3 -4 1 2 4] the output must be false but I get true I think the wrong is in the indexing

Best Answer

for ii = 1:(L-1)
if x(ii+1) > x(ii) % we have problems in the -ve numbers
tf= true ;
else
tf=false ;
end
end
Look at this portion of the code. For each ii in 1:(L-1), this portion of code gets called. This means that the value of tf is being updated 'L' number of times (i.e., when checking every pair of elements).
For example, the last two elements in [ -3 -4 1 2 4] are 2 and 4, which are monotonically increasing. Therefore, on the last iteration the value of tf is set to true and is not affected by the rest of the input.
You should try rewriting your code in a way where the value of tf is only updated to false when an issue is found.