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

13 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Ag. de 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 comentarios
Hussein
Hussein el 21 de Ag. de 2021
Editada: Hussein el 21 de Ag. de 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 el 21 de Ag. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by