alternatives to create a huge 9 diagonal matrix to spdiags
2 次查看(过去 30 天)
显示 更早的评论
I have a problem I am working with a 9 diagonal linear system, what I am doing is finding the vectors of each diagonal, let me called Ai and then using spdiags function to form the coeficient matrix but my matrix is huge somenthing like 1 million x 1 million and spdiags is taking like 1.5 h just for doing the matrix is there any better way to do this. I would appraciate any help. Let me give you some code
[row,col,bands]=size(I)
Diag=[A1 A2 A3 A4 A5 A6 A7 A8 A9]; %Ai: col vector of ith diagonal
n=length(A5);%A5 main diagonal
indx=[-row-1;-row;-row+1;-1;0;1;row-1;row;row+1];
A=spdiags(Diag,indx,n,n)
回答(1 个)
Jan
2011-3-16
Perhaps this helps:
[row,col,bands] = size(I)
Diag = [A1 A2 A3 A4 A5 A6 A7 A8 A9];
n = length(A5);
indx = [-row-1;-row;-row+1;-1;0;1;row-1;row;row+1];
A = spalloc(numel(Diag), n, n);
for i = 1:9
A = spdiags(Diag(:, i), indx(i), A);
end
All A1 to A9 have the same length, correct? I've reserved more memory for A than necessary. I assume, you can fix this according to the value of "row".
Does this create the wanted result? If so, I'm not sure, why SPDIAGS is so much slower when inserting all diagonals together.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!