Problem 6. Select every other element of a vector
Solution Stats
Problem Comments
-
41 Comments
Below is my code but it is failing the test 6 and 7. I tried inputting x=randi(100,10,1) and x=primes(20) in the scratchpad and the code works fine, outputing the right answer. There seems to be a problem with the evaluation of the answers.
function y = everyOther(x)
y = [];
for i = 1:2:length(x);
y(end+1) = x(i);
end
@Muhammad, take a deeper look at what the test cases are testing for.
y = x(1:2:end);
does the trick. :)
Solution Comments
Show commentsProblem Recent Solvers34486
Suggested Problems
-
Project Euler: Problem 1, Multiples of 3 and 5
3424 Solvers
-
4138 Solvers
-
Find the sides of an isosceles triangle when given its area and height from its base to apex
2014 Solvers
-
String Array Basics, Part 1: Convert Cell Array to String Array; No Missing Values
1786 Solvers
-
1944 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!