how can i store arrays of different sizes in each for loop?

Hi guys, how can I store arrays of different sizes in each iteration of a for loop?
for example I have this code, inside the for I have a function that in each iteration returns an array of a certain size in the second iteration it returns another array of a different size and when storing it it returns an error saying that the arrays are not the same size.
how can I store these arrays of different sizes in a single array?
thanks in advance
niterations= 100;
Nusers = 120;
vec_nack= [];%to stock
vec_ack= [];%to stock
for g=1: niterations
[vec_0,vec_1]= DETECTIONALGORITHM (Nusers);
vec_nack=[vec_nack vec_0]
vec_ack=[vec_ack vec_1]
end

 Respuesta aceptada

The easiest way is to use a cell array:
niterations= 100;
Nusers = 120;
vec_nack = cell(1,niterations);%to stock
vec_ack = cell(1,niterations);%to stock
for g=1: niterations
[vec_nack{g},vec_ack{g}]= DETECTIONALGORITHM (Nusers);
end
Then you can access individual elements (which are arrays) in your cell arrays using curly brace {} indexing:
% the 3rd element of vec_nack, for instance:
vec_nack{3}

4 comentarios

hello thank you very much for the answer, but believe me I already did this, but in the end I still need all those individual arrays to be joined into a single array, there I return to the problem at the beginning, how to join all those arrays of different sizes into a single array .
And each matrix always has 4 columns, what changes in each iteration is only the number of rows.
Voss
Voss el 18 de Mzo. de 2022
Editada: Voss el 18 de Mzo. de 2022
Since they all have the same number of columns you can concatenate them vertically into a single array (rather than trying horizontal concatenation, which is what you had):
niterations= 100;
Nusers = 120;
vec_nack= [];%to stock
vec_ack= [];%to stock
for g=1: niterations
[vec_0,vec_1]= DETECTIONALGORITHM (Nusers);
vec_nack=[vec_nack; vec_0];
vec_ack=[vec_ack; vec_1];
end
(Note the semi-colon in vec_nack=[vec_nack; vec_0] vs vec_nack=[vec_nack vec_0]. With the ; is vertical concatenation. Without the ; is horizontal concatenation.)
Thank you very much, my friend
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Comentada:

el 18 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by