How do you append to a matrix within a for loop?
324 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
chels19
el 8 de Jul. de 2016
Respondida: Manuela
el 13 de En. de 2025
Hi, I am using a for loop to process data (this part works fine). What I need to do is take the matrix (A) and after each loop update A to create one matrix. For instance, in the image below A is produced on the first loop, during loop 2 A "grows" to include the data from loop 1 and the data from loop 2, and so on.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/154758/image.png)
Loop 1 produces a matrix, on the next iteration I need to append to this matrix the results of that loop, and so on until all of the data is processed.
It should be noted that the number of rows on each loop is unknown. However, the number of columns is fixed to 7.
Any ideas would be greatly appreciated please.
0 comentarios
Respuesta aceptada
Más respuestas (3)
SRIAKILAN S A
el 21 de Feb. de 2023
% make an empty array
% the main command is end+1
N=[];
for i=0:10
x=i*i;
N(end+1)=[x]
end
0 comentarios
Manuela
el 13 de En. de 2025
I modified the code of the first answer in order to obtain a matrix and not a list of cells:
your_result = [];
for ii = whatever
some_vector = some_function(of_something);
your_result = cat(1,your_result,some_vector);
end
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!