Speed up code - deletion of rows/columns

2 views (last 30 days)
Matthias Heindl
Matthias Heindl on 25 Aug 2020
Answered: Mir Amid Hashemi on 28 Sep 2020
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 Comments
KSSV
KSSV on 25 Aug 2020
Check squeeze once. First you need to think why those many dimensions in matrix?
Matthias Heindl
Matthias Heindl on 25 Aug 2020
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.

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 25 Aug 2020
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 Comment
Matthias Heindl
Matthias Heindl on 26 Aug 2020
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.

Sign in to comment.


Mir Amid Hashemi
Mir Amid Hashemi on 28 Sep 2020
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.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!