how to delete diagonal values in a matrix?

Hi everyone;
For example I got an matrix that all diagonal values are zero and I want to remove them.
A=[0 5 4 3
3 0 5 1
4 2 0 2
3 8 6 0]
I want to get a new matrix like that:
Anew=[ 5 4 3
3 5 1
4 2 2
3 8 6]
Is there any suggestion?
Regards...

3 件のコメント

Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2016 年 1 月 18 日
v=find(A==0) A(v)=[]
Guillaume
Guillaume 2016 年 1 月 18 日
Notwithstanding the fact that this will flatten A which is not what the OP is looking for, the find is completely unnecessary here.
A(A == 0) = [];
will produce the same result.
bilgesu ak
bilgesu ak 2016 年 1 月 18 日
I tried it but it gives a row vector that is:
[3 4 3 5 2 8 4 5 6 3 1 2]
I need a matrix that only diagonal values are gone but all rest is same...

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

 採用された回答

Guillaume
Guillaume 2016 年 1 月 18 日

1 投票

One possible way:
Anew = reshape(nonzeros(A'), size(A, 2)-1, [])'

5 件のコメント

Thorsten
Thorsten 2016 年 1 月 18 日
That's neat.
bilgesu ak
bilgesu ak 2016 年 1 月 18 日
Thanks Guillaume;
At last it works true.
Regards...
Guillaume
Guillaume 2016 年 1 月 18 日
編集済み: Guillaume 2016 年 1 月 18 日
Note that it assumes that the only zeros are on the diagonal. A safer solution would be:
Atemp = A';
Anew = reshape(Atemp(~eye(size(Atemp))), size(A, 2)-1, [])'
Stephen23
Stephen23 2016 年 1 月 18 日
+1 very nice solution (given zeros only on the diagonal...)
bilgesu ak
bilgesu ak 2016 年 1 月 21 日
thank you Guillaume, this solution only focuses on diagonals...

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

その他の回答 (1 件)

shubhashree bal
shubhashree bal 2021 年 10 月 14 日

0 投票

Try this:
A=[0 5 4 3
3 0 5 1
4 2 0 2
3 8 6 0];
u=1;
value =size(A,1)+1;
row_value_end=size(A,1)-1;
col_value_end=size(A,1);
for k=1:size(A,1)
u(k+1)=u(k)+value;
end
g= A.';
g(u(1:end-1))=[];
y= reshape(g,[row_value_end,col_value_end]);
b=y.';

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

質問済み:

2016 年 1 月 18 日

回答済み:

2021 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by