Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How can I solve this

1 回表示 (過去 30 日間)
ghada saber
ghada saber 2017 年 3 月 20 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
write a script that select only odd numbers from random vector of length 10 and its elements between 1 to 20

回答 (2 件)

Jan
Jan 2017 年 3 月 21 日
編集済み: Jan 2017 年 3 月 21 日
As usual for homework question, let me encourage you to try it at first by your own an then ask a specific question. The forum will not solve your homework, because this is not useful in a long term of view - neitehr for you nor for the forum.
Start with the definition of "odd": The rest of a division by 2 is 1. The rem or mod function is useful to calculate this.
Then you should extract elements in the range between 1 and 20. (Is this correct? Or should the inputs vector have this range?) Then use "logical indexing" - search in the documentation for these terms, or here in the forum. Note that "1<x<20" is not valid in Matlat, but you need 1 < x & x < 20.
Please try it and then post the code you got so far. When you ask a specific question, the forum will help you: Assistance is fine, posting solutions is not.

John BG
John BG 2017 年 3 月 20 日
編集済み: John BG 2017 年 3 月 21 日
Hi Ghada
x=randi([1 20],1,10)
x_odds=nonzeros(x.*mod(x,2))' % find odd parity values of x
x_odds_locations=find(mod(x,2))
x =
12 15 1 10 16 8 2 6 14 17
x_odds = 15 1 17
x_odds_locations = 2 3 10
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  2 件のコメント
James Tursa
James Tursa 2017 年 3 月 20 日
This solution does not work and generates an error because of invalid indexing in x_odds=x(mod(x,2)). Also, since this is obviously homework, it is best in this forum to encourage the poster to make an attempt and offer advice/corrections on his/her attempt rather than giving a complete solution outright.
John BG
John BG 2017 年 3 月 21 日
syntax amended, thanks
John

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by