How can I multiply two big matrices, avoiding out of memory?
조회 수: 15 (최근 30일)
이전 댓글 표시
for example, I = A*B, in which size(A) = [1024^2, 3], size(B) = [3, 1001^2]. So, size(I) = [1024^2, 1001^2] which could cause out of memory.
I have tried using tall arrays like the following code:
AA = tall(A);
II = AA*B;
I = gather(II);
but the command line still shows error: out of memory.
Sincerely thanks!
%% update %%
Thanks for your answer very much! Originally, I want to calculate this equation:
![微信截图_20190826095243.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235275/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20190826095243.png)
where
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235276/image.png)
so I write the following matrix equation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235277/image.png)
m and n are variable, for example,m = 1024, n=1001^2 whatever. It is worth noting that the two matrices in exp(...) are not too big, but the result of their multiplication is too large to be storaged in memory. I've tried to split the two matrices into several small patches, but it need to input the number of the patches manually rather than automatically, moreover, the for loop could not be avoided in this case.
I have no good ideal currently.
Best regards!
댓글 수: 2
Bruno Luong
2019년 8월 25일
편집: Bruno Luong
2019년 8월 25일
Storage of your matrix requires 8 Terabytes. Do you have a HD that big?
채택된 답변
Bruno Luong
2019년 8월 26일
편집: Bruno Luong
2019년 8월 26일
You can process by chunk of smaller (100 here);
% Generate small test data
m = 100;
n = 100^2;
C = rand(m^2,3); % your [alpha,beta,gamma]
XYZ = rand(3,n); % your [x; y; z];
A = rand(1,m^2);
psize = 100; % chunk size
E = zeros(1,n);
count = 0;
while count < n
p = min(psize,n-count);
j = count+(1:p);
E(j) = A*exp(C*XYZ(:,j));
count = count + p;
end
댓글 수: 4
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!