Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise.
Show older comments
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
Accepted Answer
More Answers (1)
D. Plotnick
on 28 Jun 2018
Edited: D. Plotnick
on 28 Jun 2018
0 votes
One idea: dx = diff(x); tf = ~any(dx<=0);
Note this will also work on matrices, if you want. E.g. to look at each row
x = {some numbers}; dx = diff(x,2); tf = ~any(dx<=0,2);
Here, tf will return a column of logicals.
Categories
Find more on Structural Mechanics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!