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