How to pass big Matrices to function fastly?
12 views (last 30 days)
Show older comments
BananaBandana
on 6 Jun 2020
Commented: Walter Roberson
on 7 Jun 2020
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
Accepted Answer
Walter Roberson
on 6 Jun 2020
MATLAB uses "copy on Write" in most cases. A pointer to a variable is passed along with a reference count. When the function goes to change the content of the array it checks the reference count.
If the reference count is two or more, then there is a second path to the same data and you cannot just change it because the other path expects it to stay the same. In this case, matlab takes a copy of the array inside the function, splitting it off from the other references to the same data, and then the function can modify the new copy of the data.
In some cases there is no second path to the data (reference count is 1) and in that case the function can modify the content without having to make a copy For example
f(rand(1,10)<=0.3)
then the results of the expression have not been assigned to a variable and there is no other way to reach them, so the array can be modified directly inside f.
There is one situation that matlab can sometimes optimize:
if you have a function of the form
function variable1 = Name(list including variable1)
then in that particular case of the same variable being input and output, and the calling function using the same input and output variable names in those positions, then matlab can sometimes modify the variable in place without making a copy.
A direct feature to pass in a pointer and modify directly, runs into the problem that variable contents are shared for efficiency. For example
x = rand(1e7);
y{3} = x;
Then matlab does not make a copy of the content of x to store in y: y{3} will be a pointer to the same storage as x until something happens that requires that they diverge.
More Answers (1)
Image Analyst
on 6 Jun 2020
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!