How to use for loop for iterations.

180 views (last 30 days)
for i=1:3
a=rand(3,1);
b=5.*(a.^2);
c=[a b]
end
At the end i get a value of c that is of the order of 3x2, that is the final value that comes after3rd iteration. So value of c gets changed in each iteration. How I can get all value of c of the loop. In other words i want to get c of the order 9x2 (3 iterations and three values for each iteration).

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 10 Dec 2014
Edited: Azzi Abdelmalek on 10 Dec 2014
c=[];
for i=1:3
a=rand(3,1);
b=5.*(a.^2);
c=[c;a b]
end
%or
c=zeros(9,2);
ii=1
for i=1:3
a=rand(3,1);
b=5.*(a.^2);
n=size(a,1);
c(ii:ii+n-1,:)=[a b]
ii=ii+3;
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!