Problem 16. Return the largest number that is adjacent to a zero
Solution Stats
Problem Comments

4 Comments
Two more tests:
1. Input x = [4 2 0 4 1 0]
Output y is 1.
2. Input x = [9 3 0 0 5 0 2 1]
Output y is 2.
Good comment, Gaik. A lot of solutions (including mine) will break with two consecutive zeros.
Why: "Input x = [9 3 0 0 5 0 2 1] Output y is 2."???
Output should be 0. It doesn't say anywhere that the number adjacent to a zero must be nonzero!
This was challenging
Solution Comments

2 Comments
nice
good idea

1 Comment
Very interesting problem

3 Comments
I understand how this function works, but how did you know to use [1 0 1] for the convolution?
cool
You want the values before and after the 0, but not the zero itself, so each true should spread out one "cell".

1 Comment
Quiet hard, but satisfying! :)

2 Comments
It is really cool to see someone has the exact chain of thoughts as you .... !!!
I believe size(x,2) is equivalent to length(x). Great solution though! Made me realize where I put in too much work.

1 Comment
Because of 2 bytes space charactor

1 Comment
Damn that's a lot higher score than I was expecting.

1 Comment
Trying to emulate imdilate.

1 Comment
Hi everyone. I realized this code is very large and cannot be run on this website because of the many loops that it has to go through. It works on matlab so if anyone could help me keep the general form of it but have it cut down in the number of loops that would be great. Thanks.

1 Comment
the imdilate function apparently can't be used... this is a clumsy clumsy workaround

1 Comment
This one was hard. Tried my best but couldn't get it shorter

2 Comments
I think the test suite needs a test for a = [0 0 0 0]. I passed with the above code but would not pass this case.
I ended up with the same logic, I'm glad I wasn't the only one.
It's a shame this is basically a verbose reworking of the imdilate function (which is not accessible for this challenge)

1 Comment
well, this looks a bit.. long?

1 Comment
I suppose overt is more at it. This will catch multiple consecutive zeros. I suppose I could have put comments too. Not to mention it's clocking 10^6sec. Why do these Cody problems go by size isn't speed incredibly more important in computational programming.

1 Comment
regexp should really be banned from problems not related with word handling...

1 Comment
smart solution.

2 Comments
This solution gives the wrong value if the largest value is at the end of the vector. For example for x = [1 0 2 0 3] it returns 2 instead of 3.
Thanks for the tip!

1 Comment
It can handle adjacent zeroes.

1 Comment
No way that the leading solution is the least complex one! The rating function is questionable.

1 Comment
Fails when the max value output should be negative and the last value of the input vector is zero, e.g. a = [5 4 1 0 2 0 5 0]. Should return 1, but returns 0.

1 Comment
this should work

1 Comment
Nice! It's quite clever to put the front and rear in pairs.

1 Comment
Peter's comment about Solution 20356 appears to apply here as well.

2 Comments
Problem Recent Solvers4695
Suggested Problems

Reverse the Words (not letters) of a String
408 Solvers

Return unique values without sorting
831 Solvers

884 Solvers

584 Solvers

771 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!