Andi
Andi

Reputation: 4855

MATLAB: Matrix multiplication with very large arrays

I need to perform a matrix multiplication with very large matrices, something like 5000x13 * 13x2000000. This leads to an error message as I don't have enough memory. I understand this. Now, what's the best strategy to overcome this memory issue?

Upvotes: 1

Views: 861

Answers (2)

Laleh
Laleh

Reputation: 508

My suggestion is to split the array that you want to generate. Even if you generate the array, you can not store it! 5000 by 2 million is larger than array size limit in MATLAB! This limit applies to the size of each array, not the total size of all MATLAB arrays. So the problem is not coming from multiplication. My suggestion is that you make four blocks of your output matrix each 5000 by 500K and write the multiplication of each block separately.

Upvotes: 1

Poelie
Poelie

Reputation: 713

You could use tall arrays. They have some quirks regarding order of multiplication and such so you may want to look up the documentation. You weren't very specific on what you want exactly, so lets say you want to find the mean of your matrix multiplication. Here's how you could do it with tall arrays:

a = rand(5000,13).';
b = tall(rand(13,2000000).');
c = b * a;
d = mean(c,1);
e = gather(d).'; 

Note that in tall array multiplication, only one matrix is allowed to be tall and if the tall array is multiplied with another matrix, then the tall array must come first. This is why I used transposes quite liberally.

Upvotes: 0

Related Questions