How to efficiently combine sparse matrices vertically

My goal is to combine many sparse matrices together to form one large sparse matrix. The only two ideas that I could think of were to (1) create a large sparse matrix and overwrite specific blocks, (2) create blocks individually, using vertcat to form my last sparse matrix. However, I read that rewriting sparse matrices is pretty inefficient, and I also read that vertcat is not exactly computationally efficient. (I did not want to consider using a for loop because of how inefficient they are).

What other alternatives do I need?

Change By volume, I mean “gluing” matrices together (vertically), the elements do not interact.

+4
source share
1 answer

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.

+5
source

Source: https://habr.com/ru/post/1469353/


All Articles