Create a row vectors that includes even/odds between 0 and X

7 ビュー (過去 30 日間)
Josh
Josh 2013 年 2 月 15 日
コメント済み: BAKALE MURPHY 2017 年 3 月 14 日
How can I created a row vector that includes every even/odd number BETWEEN two variables but does not include them. ie x=10 y=20 vector=[12 14 16 18]

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 15 日
x=10;
y=20;
v=x+1:y-1
v_even=v(mod(v,2)==0)
v_odd=v(mod(v,2)~=0)

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 2 月 15 日
out = (x+1) : 2 : (y-1)
or
out = (x+2) : 2 : (y-1)

John BG
John BG 2015 年 10 月 12 日
Also possible: find(mod(X,2)==0) and find(mod(X,2)~=0)
  2 件のコメント
Image Analyst
Image Analyst 2015 年 10 月 12 日
Not as-is -- you'd need to add code:
X = 10:20
oddNumbers = find(mod(X,2)==0)
gives
X =
10 11 12 13 14 15 16 17 18 19 20
oddNumbers =
1 3 5 7 9 11
which is not [11,13,15,17,19], but if you added all but the last element to the first element of the original X, it would work.
John BG
John BG 2016 年 2 月 13 日
Should have added the X(),
find(mod(X,2)~=0)
does give the positions of the odd elements of X
ans = 11.00 13.00 15.00 17.00 19.00
and
find(mod(X,2)==0)
gives the even elements of X
ans = 10.00 12.00 14.00 16.00 18.00 20.00
Alternatively
nonzeros(mod(X,2).*X)'
also gives the odd elements of X

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by