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

13 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Hussein
Hussein am 21 Aug. 2021
Bearbeitet: Hussein am 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 am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by