Hi everyone, please help,
M is from 1 to 10 ( 1,2,3....,10)
if M is odd then N = M + 1 else N = M + 2
I created this file but the answer is wrong
close all
clear all
M=1:1:10;
if (mod(M,2)==1)
N=M+1;
else
N=M+2;
end
Matlab give N = 3 4 5 6 7 8 9 10 11 12
but it should be
N = 2 4 4 6 6 8 8 10 10 12

 채택된 답변

Ben11
Ben11 2014년 7월 28일
편집: Ben11 2014년 7월 28일

0 개 추천

You're almost there!
clear
clc
M = 1:10;
N = zeros(1,length(M));
for k = 1:length(M)
if mod(M(k),2) == 1
N(k) = M(k)+1;
else
N(k) = M(k)+2;
end
end
N
N =
2 4 4 6 6 8 8 10 10 12

댓글 수: 6

son
son 2014년 7월 28일
the answer should be N = 2 4 4 6 6 8 8 10 10 12
Ben11
Ben11 2014년 7월 28일
편집: Ben11 2014년 7월 28일
oups brain freeze sorry I edited my answer. I changed mod(M,2) == 0 to mod(M(k),2) == 1.
son
son 2014년 7월 28일
it is perfect. many thanks to you.
Ben11
Ben11 2014년 7월 28일
great you're very welcome!
son
son 2014년 7월 28일
편집: son 2014년 7월 28일
one more question how to calculate the sum of all the N.
Ben11
Ben11 2014년 7월 28일
sum(N) should do it

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

son
2014년 7월 28일

댓글:

2014년 7월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by