0

I am multiplying two matrices A (of size nxn) and B (of size nxm). The simplest way in matlab will be

n = 1000;
m = 500;
for k=1:n
  A(k, :) = (1:n)+k;
end
B = rand(n, m);
C = A*B; % C of the size nxm

however, this code occupies too much of memory when n and/or m too big. So I am looking for a vectorize version of array to implement that

n = 1000;
m = 500;
B = rand(n, m);
func0 = @(k, colv) [(1:n)+k]*colv;
func1 = @(V) arrayfun(func0, 1:n, V);
func1(B)

but it doesn't work. It said the dimension doesn't match up. Anybody has any suggestion?

1
  • Can you explain again why you find C = A * B inadequate? Commented Jul 16, 2013 at 9:26

1 Answer 1

1

I would not use anything fancy for this, just break down the linear algebra being performed.

C = zeros(n,m);
for k = 1:n
    C(k,:) = ((1:n)+k)  *  B;
end

Or, slightly more verbosely

C = zeros(n,m);
for k = 1:n
    A_singleRow = ((1:n)+k);
    C(k,:) = A_singleRow*  B;
end

For crazy-big sizes (which it sounds like you have), try reformulating the problem so that you can iterate on columns, rather than rows. (Matlab uses column-major matrix storage, which means that elements in the same column are adjacent in memory. Usually thining about this falls into the realm of over-optimization, but maybe not for you.)

For example, you could construct Ctranspose as follows:

Ctranspose = zeros(m,n);  %Note reversed order of n, m
Btranspose = B';          %Of course you may want to just create Btranspose first
for k = 1:n
    A_singleRowAsColumn = ((1:n)'+k);
    Ctranspose(:,k) = Btranspose * A_singleRowAsColumn;
end

The tools arrayfun, cellfun are very useful to functionalize a for loop, which can be used to make code more clear. However they are not generally useful when trying to squeeze performance. Even if the anonymous function/arrayfun implementation was debugged, I suspect it would require roughly the same memory usage.

Sign up to request clarification or add additional context in comments.

3 Comments

you are right, the performance is really not as fast as directly using matrix multiplication in matlab. But the memory used is less than the original case. In my code, the matrix is so big (used more than 30GB or even more), either your code or arrayfun will save 1/3 memory. But for efficiency, using a loop is not as fast as arrayfun (though it is not easy to understand). My code is simulating a big matrix, it totally uses up to 100GB and run more than 500hours, it is too big and too slow to run with direct matrix multiplication, so I need an alternating scheme
That's kind of crazy big. I added another thing to try, related to column-major ordering in Matlab. In addition, make sure that you actually fit into your physical memory. Run [~,x]=memory, and make sure that x.PhysicalMemory.Available is comfortably large.
Thanks. Yes, it does fit into my memory. The code was running in a cluster has BIG memory but only partial of memory will assigned to each user and task, so I am finding a way to balance the memory and performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.