#Day97#100DaysChallenge- Matlab Loops |Determine whether a vector is monotonically increasing-Matlab Cody
#Day97-Determine whether a vector is monotonically increasing-Matlab Cody
Task:
Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise.
A=[1,2,3,4,5]
True
B=[0,-3,4]
False.
Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.
Matlab Code:
function tf = mono_increase(x)
for i=2:1:length(x)
if x(i-1)>x(i)
tf=logical(false);
break;
else
tf=logical(true);
end
end
end
Sample Input and Output
x = [0 1 2 3 -4];
false
x = [0 1 2 3 4];
true
Click here for video description
Free Codes: youtube.com/castorclasses
No comments