Check if vector is increasing monotonely or just increasing

조회 수: 64 (최근 30일)
am
am 2019년 3월 22일
편집: Rik 2023년 2월 2일
Hello,
I have to check if the elements in a vector are in increasing order (or even equal to each other). In that case the function should return true. But if the following element is smaller, it should return false.
The function below doesn't pass tests if my vector is not monotonous, and I can't understand why?
function t = is_increasing(x)
k = 1
for k = 1:(length(x)-1)
if x(k+1) - x(k) > 0 || x(k+1) - x(k) == 0
t = true
k = k + 1
else
t = false
return
end
end
end

채택된 답변

KSSV
KSSV 2019년 3월 22일
편집: Rik 2023년 2월 2일
YOu should read about diff. You can search this question in google. It is asked multiple times.
Edit @Rik: replaced link with archive link, as the original website seems down.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by