create a vector of all the odds positive integers smaller than 100 in increasing order to save it into a variable

36 ビュー (過去 30 日間)
Hi im new student in matlab, i try to do this exercise but i dont understand the thing size, the test asking me for a size [1 50] and say is currently [1 99], the code i wrote was:
odds = 1:1:99
thank you
  23 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 14 日
A variable named Even with capital E and no s in the name, is not the same as a variable named evens with lower-case e and s in the name.
MUDDASAR AYYUB
MUDDASAR AYYUB 2020 年 6 月 14 日
Hi, thank you so much it worked for me. it was quite simple. thankyou
here is the code
j=1;
k=1;
for i=1:100
if mod(i,2)==0 && i~=1
even(j)=i;
j=j+1;
else
odd(k)=i;
k=k+1;
end
end
odds = [1:2:99]
evens = [100:-2:1]

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

回答 (5 件)

Michael Hawks
Michael Hawks 2020 年 4 月 3 日
You were close. You just need to change one digit from your command to look like this:
odds = [1:2:99]
The '2' in the 2nd position there tells Matlab to skip some numbers, keeping only every 2nd digit.
  2 件のコメント
Rik
Rik 2020 年 4 月 3 日
編集済み: Rik 2020 年 4 月 3 日
Since this is a homework question it is generally not a good idea to give complete working solutions. You can keep this answer now as it is, as Andres probably got an email notification with the contents of your answer.
The general consensus is to teach people how to solve their homework with the available tools (Matlab itself (trial and error), the documentation, Google, and their brains). Understanding questions and learning how to solve them is a real skill, it takes time to learn.
Also, the brackets are not needed. The colon operator already returns a vector, you don't need the brackets to make it a vector again. You will also notice mlint is giving you a warning to that effect (the orange squiggle).

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


Mayuresh Chawde
Mayuresh Chawde 2020 年 5 月 24 日
That was so tricky questions in assignments you have to be very thougro with pratica online lectures for second that is for even you need to put vector in decreasing order just like that -
evens = 100:-2::1
there you go. You solved it easily

D Deepak
D Deepak 2021 年 5 月 26 日
編集済み: Rik 2021 年 9 月 30 日
j=1;
k=1;
for i=1:100
if mod(i,2)==0 && i~=1
even(j)=i;
j=j+1;
else
odd(k)=i;
k=k+1;
end
end
  1 件のコメント
Rik
Rik 2021 年 5 月 26 日
Why the &&i~=1 part? Why are you dynamically growing arrays? Why didn't you comment your code?
And why are you posting a solution to a homework assignment? And why didn't you format it as code?

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


N/A
N/A 2023 年 3 月 14 日
移動済み: Image Analyst 2023 年 3 月 14 日
odds = 1:2:100 % create odd from 1 to 100
evens = 100:-2:2 % create evens from 0:99
  1 件のコメント
Image Analyst
Image Analyst 2023 年 3 月 14 日
I don't see where the original poster said anything about evens, or the order of the evens. But anyway, I think you meant
evens = 2 : 2 : 100; % Create vector of even numbers from 2 : 100, OR...
evens = 100 : -2 : 2; % Create vector of even numbers from 100 to 2 in reverse order.

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


Pratheeksha
Pratheeksha 2024 年 7 月 2 日
j=1;
k=1;
for i=1:100
if mod(i,2)==0 && i~=1
even(j)=i;
j=j+1;
else
odd(k)=i;
k=k+1;
end

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by