Removing entries of vector indexed by odd integer.

16 ビュー (過去 30 日間)
David
David 2013 年 9 月 25 日
So, in a question we've been asked to define a vector "x" which begins at 1.5 and ends at -3.5 with 101 entries in between without the use of any loops. So, I used linspace(1.4, -3.5, 101) to do this. However the next part of the question requires you to create a matrix "y" which corresponds to "x" only with the entries indexed by an odd integer removed. How can you do this without using a loop? Is there a function for it? Thanks.

採用された回答

Leah
Leah 2013 年 9 月 25 日
You just have to reference every other element of the vector you created.
x=linspace(1.4, -3.5, 101) ;
y=x(1:2:end);
  1 件のコメント
David
David 2013 年 9 月 25 日
Will that not include elements 1 and 101 though, and they're odd indexes? Why wouldn't it be y = x(2:2:100);?

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

その他の回答 (1 件)

Matt J
Matt J 2013 年 9 月 25 日
編集済み: Matt J 2013 年 9 月 25 日
x=linspace(1.4, -3.5, 101) ;
y=x;
y(1:2:end)=[];
or
y=x(2:2:end);
  9 件のコメント
Matt J
Matt J 2013 年 9 月 25 日
See also
>> 2:2:15
ans =
2 4 6 8 10 12 14
David
David 2013 年 9 月 25 日
Brilliant! Thanks a million.

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

カテゴリ

Help Center および File ExchangeFunctional Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by