Odd numbered elements problem

3 ビュー (過去 30 日間)
Zaki
Zaki 2013 年 10 月 3 日
コメント済み: AbdElrahman Mahran 2020 年 4 月 11 日
I have problem about Matlab. There is one question about odd numbered elements. Can you help me to solve that my problem?
This is the question :
Write a function which 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]
and
input x = [5 9 3 2 2 0 -1]
output y is [5 3 2 -1]
Thanks

回答 (2 件)

Alejandro Ayala
Alejandro Ayala 2019 年 11 月 5 日
function y = every0ther(x)
y = x(1:2:end);
end
Hope this helps!

danny
danny 2013 年 10 月 3 日
you can probably use a loop
e.g. k=1; for n = 1:length(x), if mod(n,2)==1, y(k) = x(n); k =k+1; end; end
or
just build an odd index e.g. i = [ 1 3 5 7....]; then use x(i) to get your odd outputs.
there are many other ways, indexes and loops are all i can think of atm.
  1 件のコメント
AbdElrahman Mahran
AbdElrahman Mahran 2020 年 4 月 11 日
Thank You Danny.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by