alternatives to create a huge 9 diagonal matrix to spdiags

4 views (last 30 days)
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)

Answers (1)

Jan
Jan on 16 Mar 2011
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.
  4 Comments
Maider Marin
Maider Marin on 16 Mar 2011
It takes almost the same time...I think I just found matlab boundary...I will try to avoid to create the coefficient matrix by programming PCG on my own. Thanks

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!