Borrar filtros
Borrar filtros

I need help preallocating array?

2 visualizaciones (últimos 30 días)
ME
ME el 12 de Abr. de 2015
Editada: pfb el 12 de Abr. de 2015
I need help making this array preallocated.
p = 10;
newArray = [];
array = [];
for i = 1:p
a= variable1;
b = variable2;
c = variable3;
d = variable4;
e = variable5;
array = [a,b,c,d,e]
newArray= [newArray array];
end

Respuesta aceptada

pfb
pfb el 12 de Abr. de 2015
Look at the function "zeros".
array = zeros(1,5);
newArray = zeros(1,5*p);
and, in the loop
newArray((1+(i-1)*5):(i*5))=array;
This way all you need is allocated before the loop.
I assume that the assignments of a through e come from function that have a different value for different i's.
If they are the same, it's just a tiling problem. You need "repmat".
  5 comentarios
ME
ME el 12 de Abr. de 2015
in my original loop the reason i have
myArray = [myArray array];
was so that in each iteration array in copied in new row.
pfb
pfb el 12 de Abr. de 2015
Editada: pfb el 12 de Abr. de 2015
not really in a new row.
In your code "array" is copied at the end of the row vector newArray. No new rows.
Use the semicolon operator to add new rows. You should have written
myArray = [myArray; array];
I think you should read some documentation.
Anyway, to allocate your matrix (not vector)
newArray = zeros(p,5);
Filling it, now is much simpler. This fills the ith row of newArray with the current values in array.
newArray(i,:) = array;
You should state your problem better. Apparently, what you needed was filling the rows of a matrix, all the time.
And perhaps take a look into the basics of matlab.

Iniciar sesión para comentar.

Más respuestas (0)

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!

Translated by