MATLAB Answers

# faster way to add many large matrix in matlab

40 views (last 30 days)
Haining Pan on 8 May 2018
Edited: Jan on 4 Nov 2019
Say I have many (around 1000) large matrices (about 1000 by 1000) and I want to add them together element-wise. The very naive way is using a temp variable and accumulates in a loop. For example,
summ=0;
for ii=1:20
for jj=1:20
summ=summ+ rand(400);
end
end
After searching on the Internet for some while, someone suggests it's better to do with the help of sum(). For example,
sump=zeros(400,400,400);
count=0;
for ii=1:20
for j=1:20
count=count+1;
sump(:,:,count)=rand(400);
end
end
summ=sum(sump,3);
However, after I tested two ways, the result is
Elapsed time is 0.780819 seconds.
Elapsed time is 1.085279 seconds.
which means the second method is even worse.
So I am just wondering if there any effective way to do addition? Assume that I am working on a computer with very large memory and a GTX 1080 (CUDA might be helpful but I don't know whether it's worthy to do so since communication also takes time.)
Thanks for your time! Any reply will be highly appreciated!.
##### 9 CommentsShowHide 8 older comments
per isakson on 10 May 2018
I assumed the question was about the speed of sum( .... );

Sign in to comment.

### Accepted Answer

Jan on 8 May 2018
Edited: Jan on 4 Nov 2019
The main time is spent in rand() in your example. With using ones() instead, the runtime goes from 0.71 sec to 0.25 sec on my machine.
Instead of creating the matrices explicitely, you could think of solving the problem mathematically, if the matrices are really exp(i*x+j*y). So please post the real code, not just some dummy code, whose most expensive function is not part of the real problem at all.
##### 3 CommentsShowHide 2 older comments
Haining Pan on 10 May 2018
After several days attempting, I found a very straightforward method by using 3d matrix. For example, I can use a=rand(400,400,400) to directly create such the whole pages of matrices and sum(a,3) to get the sum. For this exact problem, I used x+y to create a 2d matrix and multiplied (.*) by a 1 by 1 by (2Nmax+1)^2 matrix of j and k to have exp(j*x+ k*y), which is a 3d matrix. Then simply take the sum by sum(..,3).
This is about 3 times faster, and even 10 times faster if I used CUDA.

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!