MATLAB Answers

For loop of array

1 view (last 30 days)
Yat Sheng Kong
Yat Sheng Kong on 11 Jul 2017
Commented: Jan on 12 Jul 2017
Hi,
I have tried for loop to run a vector but having an error. Could anyone tell me how to loop the vector.
x = ones(10,1);
for k = 1:10
y(k) = x + k
end
Thanks, Kong

  1 Comment

Jan
Jan on 12 Jul 2017
Whenever you mention in the forum, that you "have an error", post a copy of the complete message. Otherwise the readers cannot guess, what the problem is.

Sign in to comment.

Answers (1)

KSSV
KSSV on 11 Jul 2017
x = ones(10,1);
y=zeros(10,1);
for k = 1:10
y(k) = x(k) + k
end
The same thing can be achieved using:
y=x+1:10;

  2 Comments

Yat Sheng Kong
Yat Sheng Kong on 12 Jul 2017
Initially, x = [1,1,1,1,1,1,1,1,1,1] and I wish to have it to become like x = [2,2,2,2,2,2,2,2,2,2] and so on.
KSSV
KSSV on 12 Jul 2017
x = ones(10,1);
y=zeros(10,1);
for k = 1:10
y(k) = x(k) + 1
end
Or
y=x+1 ;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by