How do I quickly delete array elements?

1 view (last 30 days)
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 Comments
KSSV
KSSV on 30 Mar 2017
Edited: KSSV on 30 Mar 2017
What is the condition to remove the elements? That can be achieved without loop which would be very fast..
George Hulme
George Hulme on 30 Mar 2017
Edited: George Hulme on 30 Mar 2017
The elements need to be removed so that they do not take up extra space in memory and slow down my lecturers computer. The program that I sent him before crashed his computer. Also, how would you go about calculating this without a loop?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Mar 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 Comments
George Hulme
George Hulme on 30 Mar 2017
The problem with keeping all the values is that although my computer has enough memory, my lecturers computer does not. I have also tried changing the program so that it keeps the values instead of removing them and as a result the loop took an average of 10.3 seconds to complete compared to th 7.6 seconds for the loop shown above
George Hulme
George Hulme on 30 Mar 2017
Nevermind, I managed to fix it with some code modification and use of the parallel processing toolbox. Thanks for the suggestions anyway!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!