How to pass big Matrices to function fastly?

18 ビュー (過去 30 日間)
BananaBandana
BananaBandana 2020 年 6 月 6 日
コメント済み: Walter Roberson 2020 年 6 月 7 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 7 日
Double check that you preallocated your arrays where appropriate.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 6 日
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.

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 6 月 6 日
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 件のコメント
BananaBandana
BananaBandana 2020 年 6 月 7 日
no, its just this 343000 elements, so not big then.
So you would say it has nothing to do with the passing of this (big) matrix?
Then why does it get so much slower?
Thanks, for the tip, but memmapfile is only used when working with external files, right?
Image Analyst
Image Analyst 2020 年 6 月 7 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by