If-then with a range

1 visualización (últimos 30 días)
son
son el 28 de Jul. de 2014
Comentada: Ben11 el 28 de Jul. de 2014
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

Respuesta aceptada

Ben11
Ben11 el 28 de Jul. de 2014
Editada: Ben11 el 28 de Jul. de 2014
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 comentarios
son
son el 28 de Jul. de 2014
Editada: son el 28 de Jul. de 2014
one more question how to calculate the sum of all the N.
Ben11
Ben11 el 28 de Jul. de 2014
sum(N) should do it

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by