simple for loop problem

13 次查看(过去 30 天)
Josiah Miles
Josiah Miles 2019-10-22
回答: Darshan 2023-11-8
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 个评论
per isakson
per isakson 2019-10-22
Sounds like homework. What you done so far and what's your problem?

请先登录,再进行评论。

回答(2 个)

Maz M. Khansari
Maz M. Khansari 2019-10-23
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 2023-11-8
x = [1:10]
for i=1:10
new_x(i,:) = x(i)+1
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by