simple for loop problem
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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)
2 commentaires
Jia-Cheng
le 11 Oct 2024
x = [1:10]
x_1 =0
for i = 1:length(x)
x_1= x_1 + x(i)+1
end
disp(x)
Réponses (2)
Maz M. Khansari
le 23 Oct 2019
Modifié(e) : Walter Roberson
le 11 Oct 2024
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);
0 commentaires
Darshan
le 8 Nov 2023
Modifié(e) : Walter Roberson
le 11 Oct 2024
x = [1:10]
for i=1:10
new_x(i,:) = x(i)+1
end
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!