According to Matlab's help, you can “parse” a sparse matrix using
[i,j,s] = find(S);
This means that if you have two matrices S and T , and you want (efficiently) vertcat them, you can do
[is, js, ss] = find(S); [it, jt, st] = find(T); ST = sparse([is; it + size(S,1)], [js; jt], [ss; st]);
Not sure if this is very effective ... but I guess this is not so bad.
EDIT: Using a sparse 2000x1000 matrix with a density of 1% and combining it with another having a density of 2%, the code above ran 0.016 seconds on my machine. Just executing [S;T] was 10 times faster. What makes you think that vertical concatenation is slow?
EDIT2: Assuming you need to do this using “many” sparse matrices, the following works (this assumes you want them all to be “in one place”):
m = 1000; n = 2000; density = 0.01; N = 100; Q = cell(1, N); is = Q; js = Q; ss = Q; numrows = 0; % keep track of dimensions so far for ii = 1:N Q{ii} = sprandn(m+ii, n-jj, density); % so each matrix has different size [abc] = find(Q{ii}); sz = size(Q{ii}); is{ii} = a' + numrows; js{ii}=b'; ss{ii}=c'; % append "on the corner" numrows = numrows + sz(1); % keep track of the size end tic ST = sparse([is{:}], [js{:}], [ss{:}]); fprintf(1, 'using find takes %.2f sec\n', toc);
Output:
using find takes 0.63 sec
The big advantage of this method is that you do not need to have the same number of columns in your separate sparse arrays ... all of this will be sorted by the sparse command, which will simply examine the missing columns for all zeros.