How do I quickly delete array elements?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2017-3-30
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!