why I use ver.2012b can pass all the tests,but not succeded in cody?
[b,ia]=unique(a,'first');
b=sortrows([b;ia]',2)';
b=b(1,:);
solved in one line !
nice problem
Hi, bainhome:
Cody is based on the 'current' version of MATLAB, not R2012b. Many functions are unchanged, but a few have changed (modified, new, or removed), including "unique".
You must add the 'legacy' flag to the list of arguments to preserve the behaviour of the "unique" function from R2012b and prior releases — refer to the documentation (link posted by Jakub Galecki).
—DIV
if u know a specific function its too easy.
solution comment
test comment
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.
—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 !
good!
http://www.mathworks.com/help/matlab/ref/unique.html
Why bother coding what has already been coded?
Hello, amin nazari.
Congratulations on devising your own script to satisfy the test cases.
As a point of feedback, I suggest that this function may not be robust/general enough to handle any possible combinations of values in an input vector.
On line c(1,m(ff))=0 zero is introduced as a 'marker' in the new vector c. Given that c was defined as being offset by 10000000 from a, this is equivalent to using a 'marker' value of -10000000 in the vector a.
Suppose that the input vector a were provided as [-10000000 2 2 1]. The above function would then immediately set c = [0 10000002 10000002 10000001]. Notice that a 'genuine' value of zero has occurred, which cannot be distinguished from the 'marker' value that is later introduced, and the correct result could therefore not be obtained for this input.
665 Solvers
Number of 1s in a binary string
4049 Solvers
Convert a vector into a number
531 Solvers
How long is the longest prime diagonal?
355 Solvers
Make a run-length companion vector
558 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!