How can I combine array into another array after each iteration?
13 views (last 30 days)
Show older comments
Hussein
on 21 Aug 2021
Commented: Walter Roberson
on 21 Aug 2021
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
0 Comments
Accepted Answer
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
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
More Answers (0)
See Also
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!