フィルターのクリア

Select all (non-zero) columns of sparse matrix

5 ビュー (過去 30 日間)
Quinten Rensen
Quinten Rensen 2018 年 8 月 24 日
コメント済み: Quinten Rensen 2018 年 8 月 27 日
Hi all,
Since I'm working with very large and sparse matrices I use sparse() to define these matrices. I do this by defining the index of the rows and columns and their corresponding value in separate vectors. For example, for a row i and columns j,j-1,j+1, the values in the matrix are defined according:
s = 1;
row(s) = i; column(s)=j; K(s) = 1; s=s+1;
row(s) = i; column(s)=j-1; K(s) = -1; s=s+1;
row(s) = i; column(s)=j+1; K(s) = -1; s=s+1;
%This is done for all non-zero points
K_matrix = sparse(row,column,K);
This works great when specifying distinct values. However, how can I use this sparse indexing when the value depends on values in other rows of the matrix. For example:
K(i,:) = K(i-1,:) + K(i+1,:);
i.e. I need all the values of another row in order to construct the current row, since I can't do something like this:
row(s) = i; column(s)=:; K(s) = K(i-1,:)+K(i+1,:); s=s+1;
I use this way of defining the sparse matrix since it is recommended for speed. Does anyone know how I can circumvent this problem without Matlab becoming slow when building the matrix?
Kind regards
  3 件のコメント
Quinten Rensen
Quinten Rensen 2018 年 8 月 26 日
I'm sorry, you can do that, but you will get the warning 'this sparse indexing expression is likely to be slow'. I introduced the row(s), column(s) indexing for speed (which I need, since the matrices become quite large).
Jan
Jan 2018 年 8 月 26 日
@Quinten Rensen: Posting an answer requires to understand, what you want to achieve, so please explain this. The purpose of "column(s)=:" is still not clear and it is no valid Matlab syntax.

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

採用された回答

Matt J
Matt J 2018 年 8 月 26 日
編集済み: Matt J 2018 年 8 月 26 日
The best way will depend on the specifics of which rows are independent and how the remaining rows are derived from them. However, often the way would be to fill all of the independent rows first and then use vectorized operations to fill the remaining ones simultaneously. For example, starting with the sparse matrix,
>> K=spdiags([1 0 1 0 1].',0,5,5); full(K)
ans =
1 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 1
I can fill in the even-numbered rows with the sum of their neighboring rows in a single statement,
>> K(2:2:end,:) = K(1:2:end-1,:)+K(3:2:end,:); full(K)
ans =
1 0 0 0 0
1 0 1 0 0
0 0 1 0 0
0 0 1 0 1
0 0 0 0 1
  3 件のコメント
Matt J
Matt J 2018 年 8 月 26 日
Also, it's probably better to work on columns rather than rows, transposing the final matrix so that columns become rows, if needed.
Quinten Rensen
Quinten Rensen 2018 年 8 月 27 日
Thank you very much Matt! This was what I was looking for!

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

その他の回答 (0 件)

カテゴリ

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