How can I sum a large (10000x10000) matrix quickly?
7 views (last 30 days)
Show older comments
declan
on 25 Sep 2016
Commented: Walter Roberson
on 25 Sep 2016
Hi MATLAB community,
I am trying to sum a 10000x10000 matrix of values +1, 0, and -1, and it is taking a longgg time.
I have tried both
term = sum(sum(A));
and
term = sum(A(:));
I do not get an error message, my program just gets hung up on this line of the code and never completes.
Any suggestions on an efficient way to sum all elements in this matrix?
Thanks!
0 Comments
Accepted Answer
Walter Roberson
on 25 Sep 2016
It should be fast:
>> ABC = randi([-1 1], 1000,1000);
>> timeit(@() sum(ABC(:)))
ans =
0.0003416905955
and that was about the slowest.
I also find I can speed it up marginally by having used an array of int8:
DEF = int8(ABC);
sum(DEF(:))
seems to be faster on average, but not always faster. On the other hand the time required do do the int8() in the first place is consistently longer than the time required to do the sum(), so I would not bother. (It might make sense to do this on much large matrices, in which case you would want to do it at the time the data was read in -- which could be done for textscan() but not so much for xlsread())
2 Comments
Walter Roberson
on 25 Sep 2016
On my system, matrices of 10000 by 10000, done 10000 times, would take about 6 minutes.
More Answers (1)
John D'Errico
on 25 Sep 2016
Actually, it DOES complete, or it will, eventually. It is just doing some heavy disk thrashing, swapping deeply into virtual memory. So your hard drive is probably working like mad there. Eventually it will come back to life.
Your problem is you are trying to solve too large of a problem for the memory that you have. So either you need to get more memory, or you need to work with smaller problems. Depending on how many zeros in the matrix, you might gain some by use of sparse matrices. (Don't even bother with sparse unless your matrix is at least 90% zeros. In most cases, it should be way more sparse than that, but if all you are doing are sums, 90% zeros should show you a gain.)
2 Comments
Walter Roberson
on 25 Sep 2016
A 10000 by 10000 matrix would be about 800 megabytes, which should not cause swapping on its own unless you were on a system on the low end of MATLAB's specifications of several years ago.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!