Extracting sub matrix from sparse matrix

4 ビュー (過去 30 日間)
Daniel Mckenzie
Daniel Mckenzie 2018 年 4 月 20 日
コメント済み: Walter Roberson 2024 年 12 月 24 日
I have a large n by n sparse matrix A and a set of indices I \subset [1,..., n].
I want the sub matrix B = A(I,I), but doing so directly takes a long time, as A is sparse. By directly I literally mean typing B = A(I,I)
Any suggestions?
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 24 日
I have to wonder whether you want the diagonal A(I(1), I(1)), A(I(2), I(2)) and so on, or whether you want all the combinations A(I(1), I(1)), A(I(2), I(1)), A(I(3), I(1))... A(I(17), I(24))...
In the case of wanting the diagonal, then consider calling spdiags and indexing the result.

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

回答 (1 件)

Madheswaran
Madheswaran 2024 年 12 月 24 日
Hello Daniel,
You can access elements of a sparse matrix using the syntax 'S(I, I)', where I is an array of indices and 'S' is sparse matrix. However, please note that the resulting matrix will also be a sparse matrix. If you require the result to be a dense matrix, you can convert it using the 'full' function. Below is a script that illustrates this process:
I = [1 3 5 8];
A = speye(100);
B = A(I, I); % B is a sparse matrix
disp(B);
(1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1
B_dense = full(B); % B_dense is a dense matrix
disp(B_dense);
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
For additional details, please refer to the following documentations:
  1. Accessing Sparse Matrices - https://mathworks.com/help/matlab/math/accessing-sparse-matrices.html
  2. full function - https://mathworks.com/help/matlab/ref/full.html
Hope this helps!

カテゴリ

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