how to build many sparse matrices
7 次查看(过去 30 天)
显示 更早的评论
I wish to create m = 10^5 sparse matrices of size n by n, say n = 10^4. I have been using
A = cell(m, 1);
for i = 1:m
row = ...; col = ...; val = ...; % here ... means some certain assignment in column vectors
A{i} = sparse(row, col, val, n, n);
end
But it is too slow. So I tried to use the types ndSparse (https://www.mathworks.com/matlabcentral/fileexchange/29832-n-dimensional-sparse-arrays) and sptensor (https://www.sandia.gov/~tgkolda/TensorToolbox/index-2.6.html). They do the job fast by creating m matrices all at once in 3d (n*n*m). It requires concatenating index and value vectors, where the speed is acceptable. However, I then need individual matrices for some operations that do NOT work on types ndSparse and sptensor. For example,
[R, p] = chol(A(:, :, i));
does not work. If I convert the object to Matlab sparse type as
[R, p] = chol(sparse(A(:, :, i)));
then it is even slower than creating A one by one in the for loop. Considering that Matlab does not support multidimensional sparse arrays (so I cannot reshape the abovementioned types into Matlab sparse tensor), how can I speed up creating m sparse matrices? Thank you!
1 个评论
Steven Lord
2018-9-13
How are you planning to use those 1e5 sparse matrices later in your code? I want to see if it is possible to reduce the number of sparse matrices you need to create while still achieving your ultimate goal by using a different approach or algorithm.
采纳的回答
更多回答(2 个)
Matt J
2018-9-13
编辑:Matt J
2018-9-13
It might also be a good idea, instead of constructing a 3D sparse array or a cell array of separate matrices, to instead create a big block diagonal matrix, where each n x n matrix is one of the diagonal blocks. That way you can do the entire Cholesky decomposition in a single call to CHOL.
4 个评论
Christine Tobler
2018-9-13
The sparse function will often be faster if the second input, col, is sorted in ascending order. If you can cheaply construct col in a way that this is the case, that should help a bit with performance.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!