matlab:提取大型稀疏矩阵的块对角线 [英] matlab: extract block diagonals of large sparse matrix
问题描述
我有一个大的稀疏矩阵A
,我想创建一个A的3X3块对角线的稀疏矩阵.我该怎么做?请记住,A
非常大且稀疏,因此任何使用迭代的方法都将变慢,并且使用某些方法创建完整(而不是稀疏)矩阵的任何方法都将占用过多的内存.
I have a large sparse matrix A
, and I would like to create a sparse matrix of the 3X3 block diagonals of A. How would I do this? keep in mind that A
is very large and sparse, so any methods that use iteration will be slow, and any methods that use some methods that creates full (as opposed to sparse) matrices will take up too much memory.
推荐答案
如果我理解正确,下面是一些代码(请参见%%%%%%%%%%%
行之间的部分.以下是计时结果,尽管for
循环.唯一的窍门是使用spalloc函数,您可能需要针对应用程序进行调整.
If I understand correctly, here is some code (see the portions between the %%%%%%%%%%%
lines. Below are timing results, which seem reasonable to me, despite the for
loop. The only trick is the use of the spalloc function, which you may have to tune for your application.
for N= [(3:3:12) (15:600:9000)]
bigsparse = sprand(N,N,0.1);
tic;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
origSize = size(bigsparse);
diagSize = 3;
numDiags = size(bigsparse,1)/diagSize;
assert(numDiags == floor(numDiags))
bigsparse_diagonals = spalloc(origSize(1), origSize(2), ceil(prod(origSize)*0.1));
for ix=(1:numDiags)-1
ixsCurrent = ix*diagSize+[1:diagSize];
bigsparse_diagonals(ixsCurrent,ixsCurrent) = ...
bigsparse(ixsCurrent,ixsCurrent);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(1,'%5d size --> %6.5f seconds \n', N, toc)
end
计时结果(请注意,生成随机测试矩阵实际上比重新格式化要花费更长的时间):
Timing results (note, it actually takes a lot longer to generate the random test matrix than to do the reformatting):
3 size --> 0.00135 seconds
6 size --> 0.00014 seconds
9 size --> 0.00013 seconds
12 size --> 0.00014 seconds
15 size --> 0.00015 seconds
615 size --> 0.00392 seconds
1215 size --> 0.00874 seconds
1815 size --> 0.01537 seconds
2415 size --> 0.02570 seconds
3015 size --> 0.03595 seconds
3615 size --> 0.05007 seconds
4215 size --> 0.06420 seconds
4815 size --> 0.08690 seconds
5415 size --> 0.10077 seconds
6015 size --> 0.13322 seconds
6615 size --> 0.14923 seconds
7215 size --> 0.17562 seconds
7815 size --> 0.37371 seconds
8415 size --> 0.23060 seconds
这篇关于matlab:提取大型稀疏矩阵的块对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!