Problem 6. Select every other element of a vector
Show older comments
Thats the question:
Write a function which returns every other element of the vector passed in. That is, it returns the all odd-numbered elements, starting with the first.
Examples:
Input x = [1 3 2 4 3 5]
Output y is [1 2 3]
Input x = [5 9 3 2 2 0 -1]
Output y is [5 3 2 -1]
Thats the code I wrote:
function y = everyOther(x)
y=[];
t=0.5*length(x);
for n=1:t
y=[z x((2*n)-1)]
end
What is wrong with it? Can you help me? :)
Accepted Answer
More Answers (1)
Afsal
on 4 Sep 2024
0 votes
function y = everyOther(x)
y = x(1:2:length(x));
end
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!