Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise.
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
D. Plotnick
2018년 6월 28일
편집: D. Plotnick
2018년 6월 28일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Structural Mechanics에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!