Problem 10. Determine whether a vector is monotonically increasing
Solution Stats
Problem Comments
-
32 Comments
I think test 2 is wrong. Is x=0 monotonically increasing? I think not.
I think test 2 is wrong. Is x=0 monotonically increasing? I think not. Why is assertion made against true then?
Does anyone know why this is not working? The code looks chunky, but it should work. Thanks!!
string1 = ['tf is true'];
string2 = ['tf is false'];
tf = diff(x);
w = numel(find(tf==0)) + numel(find(tf<0));
if w > 0
disp(string2)
else
disp(string1)
end
[0] is monotonically increasing, but [0 0 0 0 0] is not. That's messed up. I agree with Kye Taylor.
All solutions with size 11 use the regexp cheat.
[0] isn't going anywhere by definition. It makes no sense that it's 'increasing'.
I agree that [0] should give false by this function, it's not monotonically increasing or increasing at all, right? I also think the test suite should include some vector like [-1 2 3 3] that is sorted but not mon. increasing.
Monotonically increasing means that it is never strictly decreasing. [0] is monotonically increasing for this reason, as is any constant vector. This problem should be restated as: Determine if the vector x is "strictly" increasing.
Nice one :D
between 'monotonic' and 'strictly' increasing.
good
Test 3 is incorrect isn't it? By definition, the following is monotonic, and hence also monotonically increasing:
x = [0 0 0 0 0];
One easily understandable size 15 solution is (use rot13 to read it):
shapgvba gs = zbab_vapernfr(k)
gs = vfrdhny(k, havdhr(k));
raq
nice
Good question
for i= 1:(length(x)-1)
if (x(i)< x(i+1))
disp('true');
else
disp('false');
end
end
it is providing correct result in my pc but not accepting here
@Sankari Name, Dawg, replace "disp('false')" and "disp('true')" with "tf=false" and "tf=true" respectively. see below,
x=[0]
for i= 1:(length(x)-1)
if (x(i)< x(i+1))
tf=true
else
tf=false
end
end
but your code wont pass the test where x=[0]
The [former] second test case of x = [0] has been removed due to its tendency to cause confusion.
Also, the test case with x = [0 0 0 0 0] has been removed since it was originally marked as false. Technically, a monotonic function "is either entirely nonincreasing or nondecreasing" per Wolfram.com. Per this definition, that answer should have been true, as it would also be true for monotonically decreasing.
nice
can someone explain to me why this code is not good for this problem
function tf = mono_increase(x)
for i:1:(length(x)-1)
if x(i+1) > x(i)
tf = "true"
else
tf= "false"
end
end
end
@Ongun Palaoglu:
#1, you are returning strings of true and false instead of true or false values. Do not use " in the solution.
#2, you are returning a true or false value for each comparison, with each prior result being overwritten. Only the last value will be returned by the function.
Single line solution.
a little bit difficult
function tf = mono_increase(x)
tf = false;
for ii=2:length(x)
if x(ii-1)< x(ii)
tf=true;
end
end
end
why i have 2 assertion errors i don't get it?what i's wrong
@Gizem, because your code only a partial condition of what is asked.
interesting one
basic algebra question.
I can't use diff function becasue it contain 'if'. What the hell
@Xingkai, that issue has been resolved and your solutions have been rescored.
Why is elseif banned? There is nothing it can accomplish that if cannot.
@Brandon People unfortunately abuse if/elseif to hard-code solutions for the test suite instead of actually solving problems. That said I agree with you that outright banning these language constructs in the test suite is almost always excessive.
Solution Comments
-
1 Comment
ngu
-
1 Comment
Can be improved
-
1 Comment
very nice!!!
-
1 Comment
Good problem!
-
2 Comments
The examples used in the question are misleading. They make you think that the result must be in string format.
That's an absurd statement.
-
1 Comment
simpler than it looks
-
2 Comments
tough problem
good problem
-
1 Comment
good problem
-
1 Comment
nice
-
1 Comment
Nice problem. You have to take care of the inequalities.
-
1 Comment
cool problem
-
1 Comment
Eh okay. I dislike this problem.
First, I used a "for loop", but the results were often not logical/ reliable. I than used
a = 1 : 1 : length(x) - 1;
if all( x(a) < x(a + 1))
tf = true;
end
and it worked. But why can't I use a "for loop"?! I feel like I did not learn anything from this
-
1 Comment
Compare the number in current index with the number in next index. If it is less than or equal to zero return false.
-
2 Comments
I truely hate this
I have got some good news for you - These solutions are not valid anymore!
-
1 Comment
nice
-
2 Comments
I don't understand your solution?
Hi Hidd, that is logical. The leading solution system is a bit broken, there are a few ways around it. That is why people are calling attention to fixing it. But as far as I know it's existed for 2+ years.
-
1 Comment
L=length(x);
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)
tf= true ;
else
tf=false ;
break
end
end
end
-
1 Comment
kinda cheated but test values were stupid
-
1 Comment
niceeeeeeeeee
-
1 Comment
in the test suite, test number 3 tests for vector [0 0 0 0 0] if it is monotically increasing, but in the assertion it claims it is false, meaning it is not monotically increasing, which i dont think is true.
the assertion should check it as true.
this is from wikipedia:
[ A function is called monotonically increasing if for all x and y such that x <= y one has f(x)<=f(y) ]
the (issorted) function should be enough to check for monotically increasing.
-
1 Comment
-0--
-
1 Comment
How is/isn't x = [0]; monotonically increasing vector when there is no other element to compare?
*Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise.*
-
2 Comments
Good pblm ..try with length(unique(x))==length(x) and all(diff(x))>=0)).tis shd be the hint for the pblm
Thanks for the hint
-
1 Comment
nice question
-
1 Comment
you are using MATLAB in a C++ way lol
-
1 Comment
nice solve!
-
1 Comment
One easily understandable size 15 solution is (use rot13 to read it): shapgvba gs = zbab_vapernfr(k) gs = vfrdhny(k, havdhr(k)); raq
-
2 Comments
-
2 Comments
Did it in one line...but took me a while to figure it out. Matlab n00b
Nice one ☺
-
1 Comment
is that guy real :'D
-
1 Comment
Can anybody helpe me? What's wrongs with this?
-
3 Comments
What's wrong? I tried it on the matlab desktop,it's okay.
same problem here
sort(x) will always return a increasing vector.
So all(diff(sort(x)))>0 is always true, if x has just unique elements.
try all((diff(x))>0)
-
1 Comment
great answer
-
1 Comment
(^-^)v
-
1 Comment
function tf = mono_increase(x)
tf = false;
if x(1,1)>=y(1, length(x))
fprintf('false \n');
elseif x(1,1)==0
fprintf('true\n');
else
fprintf('false\n');
end
end
%this is compiling in matlab with all inputs
-
2 Comments
Could someone help me? I don't know why it's wrong.
You are returning the character strings 'true' and 'false' instead of the logical values true and false (so just remove the quotes).
-
2 Comments
This solution code must have sth wrong. When i run this code on my matlab, it will give the right answer however when I run it on this web, it doesn't work
true and false are booleans. 'true' and 'false' are strings. It's not the same thing.
-
2 Comments
Can't improve more. Any help (trick) is appreciated...
the sign function is not needed
-
2 Comments
Nice problem
I agree
-
2 Comments
I don't understand why this solution is wrong, can I get some help please?
This code returns the strings 'true' and 'false' instead of the logical values: true and false.
-
3 Comments
Where did I do wrong?Please help me!
You fail test 2, since the vector x has length 1, you never evaluate anything in your loop. This can be fixed by initializing tf before the loop.
Look into recursive solution instead of this loop.
Why are you incrementing the solution with i=i+1? The for loop automatically increments from 1 to n-1. You are also only really testing the last two values in the vector. Your function returns false for each pair in vector but overwrites tf = false with true when it reaches the last two entries. You need to break out of the loop if you detect one false.
-
3 Comments
This doesn't solve the problem...
lots of things dont.
cheater.
-
1 Comment
sahi hai...
-
2 Comments
-
1 Comment
this is the solution right???
-
2 Comments
-
2 Comments
where is the problem here? it gives coreect result in matlab
tf should be TRUE or FALSE, not a string
-
1 Comment
I dont see any problem in matlab. what is the problem here ?
thanks
-
3 Comments
Can anybody explain me, why this solution is smaler than the version where x = diff(x); is saved befor?
I mean:
x = diff(x);
tf = all(sign(x) > 0) || isempty(x);
all(diff(x)>0)
you can reduce your score by 6 points by removing the logic after ||.its taken care by 'all'
-
2 Comments
-
4 Comments
The leading solution is wonderful!!!
By the looks of this...
I have a long way to go.
Impressive.
(^^)>
how?
-
1 Comment
This is simultaneously abusive and beautiful.
-
1 Comment
good question
-
1 Comment
don't know how I missed "isequal" the first time around... it's right there in the test suite. DUH.
-
1 Comment
why this solution is not right? In the fist case, it returns 16, isn't it equal true?
-
1 Comment
Why does regexp work like this? Is it a bug? If so, could the Cody team please fix this so that players can't use it to score easy points?
-
1 Comment
This is just gaming the test set for a limited program with an artificially low score...
-
3 Comments
so I did it in 1 single line but my size is too big :((
Here's my one-liner (similar to yours):
tf = ~sum(x(2:end) - x(1:end-1) <= 0)
you did good job
-
1 Comment
assertion 2 of the test suite is monotonically increasing (x = [0])? Don't you have to have at least two numbers?
-
1 Comment
Nice shot!!!
-
1 Comment
Didn't think about using all , well played.
-
3 Comments
Good function of unique!
Nice :)!
excellent
-
1 Comment
Really an awesome problem yaar!!
-
1 Comment
assert(monotonic increasing == nondecreasing,'check the definition')
-
1 Comment
I think test 2 is wrong. Is x=0 monotonically increasing? I think not.
-
1 Comment
i dont know why this is not working .
-
2 Comments
OK, I had to test the leader's work myself, and I get it now. If any values repeat, unique()
's answer is shorter and therefore not equal. Since unique()
sorts the answer in ascending order, only a strictly increasing function has both terms equal.
can I say: WOW
-
1 Comment
Clever although slow
-
2 Comments
Why doesn't this fail on test 2? Why should diff(0) > 0 be true?
The weirdness is due to this: all([]) is true.
Problem Recent Solvers16913
Suggested Problems
-
4780 Solvers
-
367 Solvers
-
514 Solvers
-
3499 Solvers
-
405 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!