How can I sum a large (10000x10000) matrix quickly?
Show older comments
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!
Accepted Answer
More Answers (1)
John D'Errico
on 25 Sep 2016
1 vote
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
declan
on 25 Sep 2016
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.
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!