MATLAB: Large Sparse Matrix Summation

large sparse matrix summationMATLAB

Hi, Everyone:
Suppose I have a very large M*N sparse matrix A, where M=K*N, I need to equally split it into K N*N matrices and sum it up, I can't use loop, so I tried to use:
However, this command can't reshape sparse matrix into a 3-D array, is there any other way to do it? (without loop or converge to dense matrix)
Many Thanks

Best Answer

This seems to work well:
% Making some random data...
N = 1000;
K = 100;
A = sprand(N*K,N,0.0001);
[r,c,val] = find(A);
rnew = mod(r-1,N)+1; % Shift all the row values to [1-->N]
Asum = accumarray([rnew c],val,[N N],[],[],true); % The answer