How do I quickly delete array elements?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
George Hulme
am 30 Mär. 2017
Kommentiert: George Hulme
am 30 Mär. 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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 30 Mär. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!