- You need to return true/false - not as string
- You compare values to be bigger or equal -> [1 1 1] is not increasing, but your solution return true
- Your solution wont work with single values, because x(i+1) does not exist
Problem 10 of MATLAB cody challenge
4 views (last 30 days)
Show older comments
I was trying to solve this question in cody challenge: Problem 10. Determine whether a vector is monotonically increasing. I tried following code:
i=1;
whilei
ifx(i)<=x(i+1)
tf ='true';
else
tf ='false';
break;
end
%
i=i+1;
end
When I am running this piece of code on MATLAB editor everything is Ok. But when I am submitting this, incorrect answer results. Format to make a function for this problem is given as:
functiontf = mono_increase(x)
tf = false;
end
Can anyone sort it out?
0 Comments
Accepted Answer
Dennis
on 30 Jul 2018
There are a few problems with your solution:
A working solution based on your approach might look like this:
iflength(x)==1
tf =true;
else
i=1;
whilei
ifx(i)
tf =true;
else
tf =false;
break;
end
%
i=i+1;
end
end
More Answers (2)
Sriram Nayak
on 2 Feb 2020
i=1;
whilei
ifx(i)<=x(i+1)
tf ='true';
else
tf ='false';
break;
end
%
i=i+1;
end
1 Comment
Guillaume
on 2 Feb 2020
I'm afraid this is is not going to work. The char array
`true`
and the logical value
true
are not the same at all.
In term of cody score
i = 1;
whilei < endbound
%do something
i = i + 1;
end
is going to score you very badly against the equivalent and much simpler
for
loop:
fori = 1:endbound
%dosomething
end
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!