Asked by Xin Liu
on 25 Aug 2019 at 14:21

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:

where

so I write the following matrix equation:

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!

Answer by Bruno Luong
on 26 Aug 2019 at 6:34

Edited by Bruno Luong
on 26 Aug 2019 at 6:51

Accepted Answer

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

Bruno Luong
on 26 Aug 2019 at 11:58

There is nothing wrong with using loop. This is the most often miss-prejudice that average MATLAB users might have about using for-loop. The for-loop by itself is OK, what you do and how you do inside the for-loop matters.

In in the case like that, it avoid you to buy a HD of 10 Tb to do your calculation.

To prove the point run this by yourself and see how much you lost.

% 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

tic

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

toc % Elapsed time is 0.546385 seconds.

tic

E = A*exp(C*XYZ);

toc % Elapsed time is 0.545368 seconds.

Xin Liu
on 26 Aug 2019 at 12:35

Well, I think I should accept your answer, :), haha~thank you very much.

Bruno Luong
on 26 Aug 2019 at 12:42

Well if you have considered this option, then it is your answer too.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Bruno Luong (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/477500-how-can-i-multiply-two-big-matrices-avoiding-out-of-memory#comment_738823

## Xin Liu (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/477500-how-can-i-multiply-two-big-matrices-avoiding-out-of-memory#comment_738914

Sign in to comment.