필터 지우기
필터 지우기

I'm trying to create a n by n sparse matrix with 3*xi-xi^2 on the main diagonal of K, and with with 6*xi on the sup-diagonal of K , where i starts from 1 to n. And everywhere else 0. Can anyone please tell me how to get that ?

조회 수: 3 (최근 30일)
Before I created a matrix that the main diagonal of K are alternatively 2 and -2’s, the sub- and sup-diagonal of K alternatively 1 and -1’s, and everywhere else 0.
n = 5;
k = mod(1:n,2)*2 - 1;
A = diag(k*2) + diag(k(1:n-1),-1) + diag(k(1:n-1),1)
But since now the number on the diagonal has to be different. I can't use this approach.

답변 (1개)

Steven Lord
Steven Lord 2017년 4월 17일
Consider building the index and data vectors and calling sparse once using those vectors. See the "Accumulate Values into Sparse Matrix" example on the documentation page for an example that builds a sparse matrix using those vectors. You'll have two "pieces" of each vector: one piece for the main diagonal and one piece for the super-diagonal.
Alternately, use the spdiags function.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by