I have a huge matrix to A to solve for Ax=B. There are some rows with zero elements in A and I want to modify the diagonal for these rows. So how can I fast find these rows with all zero elements? for loop is rather slow.

2 ビュー (過去 30 日間)
I have a huge matrix to A to solve for Ax=B. There are some rows with zero elements in A and I want to modify the diagonal for these rows. So how can I fast find these rows with all zero elements? for loop is rather slow.

回答 (2 件)

KSSV
KSSV 2019 年 3 月 13 日
編集済み: KSSV 2019 年 3 月 13 日
A = [0 0 0 ; 1 2 3; 4 5 6] ;
idx = sum(A,2)==0
A(idx,:)
Or Simply:
A = [0 0 0 ; 1 2 3; 4 5 6] ;
idx = any(A,2)
A(idx,:)
  2 件のコメント
yangyang qiao
yangyang qiao 2019 年 3 月 13 日
Thanks, How to set diagonal as 1 for the row with all zero elements?

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


Andrei Bobrov
Andrei Bobrov 2019 年 3 月 13 日
編集済み: Andrei Bobrov 2019 年 3 月 13 日
"How to set diagonal as 1 for the row with all zero elements?"
A = A + diag(~diag(A));
or
n = all(A==0,2);
A = A + diag(n);
or
n = all(A==0,2);
ex = (1:size(A,1)+1:numel(A))';
A(ex) = A(ex) + n;
  1 件のコメント
yangyang qiao
yangyang qiao 2019 年 3 月 13 日
thanks, your code can change the diag when diag is 0, what I hope is that change the diag when the whole row is 0

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

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by