How to pass big Matrices to function fastly?
Show older comments
Hi,
I have a function which is called many times over and over in a simulation. The function calculates and changes values of a matrix (70^3 elements) and the matrix then is plotted. Due to more readability, I moved a big chunk of the code to a new function. I also pass the big matrix to the function and then get the changed matrix back. Is there a way to do this more efficiently than just passing the variable that contains the matrix? Like in other languages just passing the address or something... Because the simulation became much slower after I've made the changes.
Thanks!
2 Comments
Walter Roberson
on 7 Jun 2020
Double check that you preallocated your arrays where appropriate.
Accepted Answer
More Answers (1)
Image Analyst
on 6 Jun 2020
0 votes
How big is the "big" matrix. Surely you don't mean the one with 70^3 elements.
>> 70^3
ans =
343000
That's only like one percent the size of a typical digital image these days, and should be no problem. So for the other, BIG matrix, are we talking hundreds of megabytes, or gigabytes? And have you looked into memmapfile()?
2 Comments
BananaBandana
on 7 Jun 2020
Image Analyst
on 7 Jun 2020
There could be many reasons. For example one common one with images is if people call hold on and then repeatedly load images with imshow(). All those prior images in the axes will stay there and the new one just gets put on top of them so the memory used grows and grows and the program gets slower and slower. Try calling hold off and cla reset right before you call imshow. Try calling memory at key points in your program to see where the memory gets used up.
Categories
Find more on Resizing and Reshaping Matrices 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!