if even/odd statement

조회 수: 21 (최근 30일)
John
John 2011년 3월 30일
Hi, I have to write a code that doubles all odd numbers, and square all even numbers from 1 to 100. Can some1 please help me out.

채택된 답변

Matt Fig
Matt Fig 2011년 3월 30일
Where are you stuck? What have you tried?
.
EDIT In response to your comment...
IF statements don't select out portions of an array like that. You will need an index to do that. Here are some clues, copy and paste into MATLAB:
x = 1:10
idx = mod(x,2)==1 % This is a logical index.
x(idx)
x(~idx)
  댓글 수: 2
John
John 2011년 3월 30일
i've try this so far but its not working..
x=1:1:100;
if a=2*x+1 %odds
a*2
else b=2*x %evens
b^2
end
John
John 2011년 3월 30일
I've got it. Thank you very much!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by