Extract part of a sparse matrix

1 回表示 (過去 30 日間)
Pedro Montenegro
Pedro Montenegro 2014 年 5 月 2 日
コメント済み: Azzi Abdelmalek 2014 年 5 月 2 日
Dear users,
I'm trying to find a way to extract a block from a big sparse matrix without being computationally costly. Here what I have
A - sparse matrix n*n i - a vector with the indices I want to extract (i = [5556 68424 85345 925325], for example)
I can do what I want like this:
A_new = A(i,i).
However, this indexing that is used in full matrices is not effective for sparse. how can I do this operation in an effective way?
Thank you in advance,
Pedro Montenegro University of Porto, Portugal

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 2 日
i = [5556 68424 85345 925325];
ii=sub2ind(size(A),i,i)
A_new = A(ii)
  2 件のコメント
Pedro Montenegro
Pedro Montenegro 2014 年 5 月 2 日
Hello Azzi,
Thank you for your answer. However, the result that I want is not the same. Lets do it with a simple full matrix for better understanding:
a=magic(5)
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
i=[2 3];
a(i,i) =
5 7
6 13
This is the answer that I want. If i run your code I'll have:
ii=sub2ind(size(a),i,i)
ii =
7 13
a(ii) =
5 13
Which is different from what i want.
Thank you
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 2 日
[ii,jj]=ndgrid(i,i)
idx=sub2ind(size(a),ii(:),jj(:))
a(idx)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by