How do I quickly delete array elements?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
George Hulme
el 30 de Mzo. de 2017
Comentada: George Hulme
el 30 de Mzo. de 2017
In my program I have 3 matrices each with roughly 300000000 elements if allowed to run to full. This takes up a very large amount of RAM and thus I have implemented the following loop:
% Loop calculates the values of X, Y and Z
for i=1:N
X(j+1,1) = X(j,1) + (s*(-X(j,1) + Y(j,1)))*H;
Y(j+1,1) = Y(j,1) + (r*X(j,1) - Y(j,1) - X(j,1)*Z(j,1))*H;
Z(j+1,1) = Z(j,1) + (-b*Z(j,1) + X(j,1)*Y(j,1))*H;
% Cuts out extra data that would otherwise fill memory and slow the
% program significantly at low H values
if mod(i,q) == 0
pop = j-q+1:j-1;
X(pop) = [];
Y(pop) = [];
Z(pop) = [];
j = j - q + 1;
end
j = j + 1;
end
The central if statement is currently active for 93% of the time that the loop is running.
I have tried changing the data type of X, Y and Z to single instead of double to speed up the program, however this changes the final result of the iterative process by an unacceptable value.
Is there any way of removing the array elements more quickly without using a C compiler or Fortran?
Any help would be greatly appreciated :)
2 comentarios
Respuesta aceptada
Walter Roberson
el 30 de Mzo. de 2017
If you have a bunch of locations to remove, it is much more efficient to mark them for removal and leave them sit until you have a fair batch to do, and then remove them all at once. If you had enough memory that would imply not doing any deletions until the end.
Also, once you have a logical keep/remove vector, it is often more efficient to copy the elements you want to keep instead of deleting the ones you do not want.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!