Speed up code - deletion of rows/columns

2 次查看(过去 30 天)
In my code I have a lot of big sized variables. In order for the code to run on my computer, I decided on deleting already assigned (and further processed, thus not anymore needed) rows / columns.
velocity_air(:,:,2) = [];
transformation_matrix(:,:,:,:,2) = [];
It seems to me, that by doing this, I succeed in reducing the required RAM, however those deleting assignments take a lot of time. In fact, the time increases exponentially when I increase the number of iterations.
Is there another way to delete used rows / columns?
What else can I do to have the code run faster?
  2 个评论
KSSV
KSSV 2020-8-25
Check squeeze once. First you need to think why those many dimensions in matrix?
Matthias Heindl
Matthias Heindl 2020-8-25
The 5 dimensions in the matrix are the exception, but I believe them to be necessary.
I animate a system of particles (balls) colliding with each other elastically. In order for balls to make elastic collisions in 3D, it is necessary to turn your reference system in such a way, that you remain with an elastic collision in 1D. This is achieved by a 3 by 3 transformation matrix, transforming from one system to another.
This matrix has to be created for between each particle of type 1 (first index) and each particle of type 2 (second index). The 3 by 3 matrix itself is resembled by the third and fourth index and the last index is time.

请先登录,再进行评论。

回答(2 个)

Steven Lord
Steven Lord 2020-8-25
I'm guessing you're doing this in a loop. In that case consider the old song 99 Bottles of Beer, slightly modified.
99 bottles of beer on the wall, 99 bottles of beer
Take one down, pass it around, slide all the other bottles forward to fill the slot, 98 bottles of beer on the wall
Doesn't quite have the same ring to it, does it? And you're going to be sliding all the bottles forward a lot. How often does that 99th bottle move? That's kind of what you're doing when you delete a page from the middle of the array.
How big are these "big sized variables"? How much memory does the system on which you're running your code have?
  1 个评论
Matthias Heindl
Matthias Heindl 2020-8-26
I'm doing it in a loop, yes.
The problem with simply executing the code was, that the variables would take up too much RAM, so I figured, it might be good, if I deleted parts of the variables on every iteration.
The illustration of yours is great. However I struggle to find a way to adapt it to my code. I use the velocity and position of particles in the last column for t = 2 to calculate those values for t = 3. Then I delete all values of position t = 2. Position t = 3 becomes t = 2 and on the next iteration I can again calculate the values for t = 3.

请先登录,再进行评论。


Mir Amid Hashemi
Mir Amid Hashemi 2020-9-28
I had a similar problem, however I don't know if it can be applied to your code. I created a fifo of constant length in the gpu. Whenever I considered one element of the fifo to have processed, I gather it to CPU ram and use that index to put a new element. What you might be doing is changing the dimension of your matrix dynamically and it slows performance.

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by