simple for loop problem

17 visualizaciones (últimos 30 días)
Josiah Miles
Josiah Miles el 22 de Oct. de 2019
Respondida: Darshan el 8 de Nov. de 2023
Create a for loop that adds one to every number in the array. For example [1,2,3] becomes [2,3,4] after the loop is complete.
a. create the variable x=1:10;
b. set the loop to run the correct amount of times
c. write the loop
d. use disp(x)
  1 comentario
per isakson
per isakson el 22 de Oct. de 2019
Sounds like homework. What you done so far and what's your problem?

Iniciar sesión para comentar.

Respuestas (2)

Maz M. Khansari
Maz M. Khansari el 23 de Oct. de 2019
The following will do the job for you.
x = 1:10;
x_new = zeros(1,numel(x));
for i=1:numel(x)
x_new(i) = x(i)+1;
end
disp(x);

Darshan
Darshan el 8 de Nov. de 2023
x = [1:10]
for i=1:10
new_x(i,:) = x(i)+1
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by