Formula for setting Diagonals

조회 수: 1 (최근 30일)
Eric Fleming
Eric Fleming 2016년 2월 29일
댓글: Walter Roberson 2016년 2월 29일
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?

채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 29일
"A = spdiags(B,d,A) replaces the diagonals specified by d with the columns of B. The output is sparse."
  댓글 수: 2
Eric Fleming
Eric Fleming 2016년 2월 29일
But in forming a matrix B (which is the main diagonal), I would still need 1000000 entries correct? I was thinking something along the lines of A = spdiags(2*i,d,A), where I wouldn't have to store a matrix B, unless I'm mistaken about something
Walter Roberson
Walter Roberson 2016년 2월 29일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by