How do I quickly delete array elements?

조회 수: 1 (최근 30일)
George Hulme
George Hulme 2017년 3월 30일
댓글: George Hulme 2017년 3월 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
KSSV
KSSV 2017년 3월 30일
편집: KSSV 2017년 3월 30일
What is the condition to remove the elements? That can be achieved without loop which would be very fast..
George Hulme
George Hulme 2017년 3월 30일
편집: George Hulme 2017년 3월 30일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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.
  댓글 수: 2
George Hulme
George Hulme 2017년 3월 30일
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 2017년 3월 30일
Nevermind, I managed to fix it with some code modification and use of the parallel processing toolbox. Thanks for the suggestions anyway!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by