How can I combine array into another array after each iteration?

13 views (last 30 days)
Hello everyone,
I am trying to learn how to combine arrays from each iteration into one. basically the set of values from X into Y. I am trying to do it with two for loops. any help in my learning experience will be appreciated.
Y=zeros(1250,1); % insert values from the inner loop to this array
for n=1:5
X=zeros(250,1); % values from 1 to 250
for i = 1:250 % i repeat 250 times different value of i each time until 250
Equa= i+ 5;
X(i) = (Equa); % store each answer from Equa to X acendingly
end
Y(n) =(X); % the stored values in x(i) to be inserted into Y after each iteration
end

Accepted Answer

Walter Roberson
Walter Roberson on 21 Aug 2021
y(n*250-249:n*250) = X;
Or better yet, just use a 2D array and reshape it if you need to:
Y = zeros(250, 5);
for n = 1 : 5
Y(:,n) = (1:250) + 5;
end
Y = Y(:);
  2 Comments
Hussein
Hussein on 21 Aug 2021
Edited: Hussein on 21 Aug 2021
Thanks Walter that works.
I am just tying to learn it with two for loops. inner loop values (from a simple formula) to be stored in outer loop. I appreciate it if you know how to do it. thanks.
Walter Roberson
Walter Roberson on 21 Aug 2021
Y=zeros(1250,1); % insert values from the inner loop to this array
for n=1:5
X=zeros(250,1); % values from 1 to 250
for i = 1:250 % i repeat 250 times different value of i each time until 250
Equa= i+ 5;
X(i) = (Equa); % store each answer from Equa to X acendingly
end
Y(n*250-249:n*250) = X; % the stored values in x(i) to be inserted into Y after each iteration
end

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!