I am working with large sparse matrices (sparse). I have a large sparse matrix of values which needs to be included into a larger sparse matrix. I have an array of logicals
which indicates which rows and columns are to be filled up with the smaller matrix values. In this application the smaller of the two matrices is a graph stored as an adjacency matrix and the logicals indicate the node id positions in the larger matrix.
A small toy example to demonstrate what I am doing currently:
zz = sparse(4,4); %create a sparse matrix of the final size desired
rr = rand(3,3); %a matrix of values
logical_inds = logical([1 0 1 1]); %a logical array to index with the dimension size of zz
zz(logical_inds,logical_inds) = rr(:,:) %'rr' values are mapped into the subset of 'zz'
I see that the 2nd column of zz
are zeros, and that the 2nd row are zero values as well. This is the output desired.
In my program get a warning that this "sparse indexing is likely to be slow", and it is. Occasionally when the matrices are very large the program terminates at this line.
How can I create this matrix (zz
) with the sparse method? I am unsure how to create the row column indexes from the mask of logicals I have, and how to turn the values of rr
into an array ordered appropriately for this new indexing.
**in general rr
is very sparse although the mask of logicals addresses the full matrix