-
4 Comments
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.
—DIV
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 !
Suggested Problems
-
10363 Solvers
-
304 Solvers
-
9505 Solvers
-
Convert a vector into a number
603 Solvers
-
530 Solvers
More from this Author96
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!