Is it possible to add/combine matrices without increasing memory usage?
2 views (last 30 days)
Show older comments
Hi. I have a script that is really pushing the RAM limit of my computer and in one situation I have two large matrices of the same size that I want to combine (one plus the other) and then remove the original matrices. So this is the situation:
C=A+B;
clear A B
But when I’m performing this calculation I’m already at 75% memory usage and during the operation it shoots up to 100% and really slows down the runtime of my calculation. So I was thinking that if instead of creating a third matrix I could overwrite one and then remove one like this;
A=A+B;
clear B
C=A;
clear A
But it still increases the memory usage. Is there a way of combining two matrices without allocating more RAM than the two matrices requires separately? Or is this the way it has to be?
0 Comments
Accepted Answer
James Tursa
on 31 Mar 2015
Edited: James Tursa
on 31 Mar 2015
MATLAB can sometimes be coaxed into performing operations in place, but there are rules you must follow. Generally, you must be inside a function when doing the operation, and you must call another function to do the work with that function specifically using in-place syntax. E.g., see this link
Your variables are large enough that you will likely benefit from this technique if you follow the in-place calling syntax and in-place function syntax. E.g.,
function plusfun_test
disp('inside plusfun_test')
A = rand(1,10000000);
B = rand(1,10000000);
disp('original A')
prpi(A)
A = plusfun(A,B);
disp('resulting A')
prpi(A)
end
function A = plusfun(A,B)
disp(' ')
disp('inside plusfun')
disp('before plus')
prpi(A)
A = A + B;
disp(' ')
disp('after plus')
prpi(A)
end
And a sample run:
>> plusfun_test
inside plusfun_test
original A
Structure = 0000000025BCA780
pr = 0000000158030060
pi = 0000000000000000
inside plusfun
before plus
Structure = 0000000025BC96E0
pr = 0000000158030060
pi = 0000000000000000
after plus
Structure = 0000000025BC96E0
pr = 0000000158030060
pi = 0000000000000000
resulting A
Structure = 0000000025BCA780
pr = 0000000158030060
pi = 0000000000000000
You can see that the real data pointer pr of A does not change throughout the calling sequence, indicating that the operation was likely done in-place (although this in and of itself doesn't prove it). (Note: prpi is a custom function of mine that simply displays the variable pointer and its associated real and imaginary data pointers)
Another alternative, if you are truly desperate, is to use mex functions for this and force the in-place operations. This can have unintended consequences if the variable is shared with another variable, however, and this technique is not officially supported in the API.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!