How can I combine array into another array after each iteration?
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hussein
am 21 Aug. 2021
Kommentiert: Walter Roberson
am 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 Kommentare
Akzeptierte Antwort
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
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!