필터 지우기
필터 지우기

how to delete diagonal values in a matrix?

조회 수: 5 (최근 30일)
bilgesu ak
bilgesu ak 2016년 1월 18일
답변: shubhashree bal 2021년 10월 14일
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
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일
One possible way:
Anew = reshape(nonzeros(A'), size(A, 2)-1, [])'
  댓글 수: 5
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일
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.';

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by