create sparse matrix from diagonal arrays

20 ビュー (過去 30 日間)
Jack Silver
Jack Silver 2014 年 4 月 10 日
コメント済み: Jack Silver 2014 年 4 月 24 日
I have the 7 diagonal that come from a 3D finite volume discretization in separate arrays. These 7 arrays have 3D shape and correspond to each elements of the stencil for all points: top, bottom, east, west, north south and central point. I would like to create a sparse matrix object from these arrays, but I feel a little lost with the spdiag documentation. Help would be gratly apreaciated.

採用された回答

Bill Greene
Bill Greene 2014 年 4 月 14 日
This doesn't exactly answer your question but still may be useful. This example:
shows how to create sparse finite difference matrices for the 1D, 2D, and 3D Poisson equation. For the 3D case, the 7-point stencil is used. For the 1D case, the use of spdiags is straightforward:
K1D = spdiags(ones(n,1)*[-1 2 -1],-1:1,n,n); % 1d Poisson matrix
Possibly you've already seen this simple example. But you'll notice that the kron function is used to create the 2D and 3D matrices. Use of spciags to create these is not nearly as straightforward as in the 1D case.
You might also find this blog useful:
As it explains, the key to efficiently creating sparse matrices in MATLAB is to pre-compute an array of row, column, value triplets and then use a single call to the sparse function to convert these to a sparse matrix.
Bill
  1 件のコメント
Jack Silver
Jack Silver 2014 年 4 月 24 日
Looks like a good start. Thanks

サインインしてコメントする。

その他の回答 (2 件)

Andrew Newell
Andrew Newell 2014 年 4 月 10 日
MATLAB supports sparse matrices, but for 3D arrays you'll need to visit the File Exchange and get N-dimensional sparse arrays by Matt J.
  2 件のコメント
Jack Silver
Jack Silver 2014 年 4 月 11 日
Sorry, but I think this is not exactly what I need. Maybe I was not clear enough (english is my third language), so lets try to be more precise :
I would like to solve a PDE using an implicit finite volume discretization. Since the problem is 3D, the matrix should be septadiagonal. I have computed the 7 diagonals (for top, bottom, east, west, north south and central point) in 7 different arrays.
Now, I would like to create a sparse matrix object from these arrays. I think that spdiag is the way to do, but I do not understand how it work
Andrew Newell
Andrew Newell 2014 年 4 月 11 日
I'm still having trouble understanding what you mean. Could you attach a file with some data?

サインインしてコメントする。


Jack Silver
Jack Silver 2014 年 4 月 24 日
Sorry for being long to get back and for the lack of clarity. I need something equivalent to this python code

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by