This solution is correct. I've checked the result for the 2. test vector a in Matlab and it gives correct value.
Hello, erqnzgaa svruehal.
On the penultimate line of your code,
b(find(b==0)) = ;
will indeed remove all zeros from the output vector.
However, in Test Case 2 the input vector itself already contains zero values. Hence, there must be exactly one occurrence of zero in the output vector from the function.
In your code there is no way to distinguish the 'genuine' zero values from the 'marker' zero values that you introduce. Hence, in the penultimate line removing all zeros leads to an incorrect result.
A quick way to fix your code would be to choose as a 'marker' something that does not appear in the input vector. For example, any of: NaN, -9999, 1234. This is not really recommended, as it is not robust to give the correct answer to any general input vector (with the possible exception of using NaN as marker under the condition that NaN in the input vector should be ignored).
Another way to fix your code would be to get rid of the code that sets elements to 0 (or any other marker value), and instead shift the code which sets elements to  (thereby directly removing those elements) so that it appears within your innermost loop.
Used the in-build function of MATLAB, this problem can be easy done .
I am wonder that the best answer is size: 10. How did him did it !
Find the treasures in MATLAB Central and discover how the community can help you!
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office