Formula for setting Diagonals
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Eric Fleming
am 29 Feb. 2016
Kommentiert: Walter Roberson
am 29 Feb. 2016
Basically, I'm creating a very large matrix (1000000 x 1000000) in which I will do operations such as finding eigenvalues on. Only certain diagonals are used, and they all follow simple formulas.
The main diagonal is just A(i,i) = 2 * i
I figured that I need to use a sparse matrix, but I don't know how to set the diagonal efficiently. A while loop that iterates through the diagonals and sets each element takes an EXTREMELY long time
Is there something along the lines of diag(2*i) or some effective formulated way to this?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 29 Feb. 2016
"A = spdiags(B,d,A) replaces the diagonals specified by d with the columns of B. The output is sparse."
2 Kommentare
Walter Roberson
am 29 Feb. 2016
You cannot give spdiags a formula or function handle: it would have to be the actual (full) vector of values
d = 0;
B = (1:size(A,1)-abs(d)).'*2;
A = spdiags(B, d, A);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!