how to delete diagonal values in a matrix?
Show older comments
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 Comments
Sivakumaran Chandrasekaran
on 18 Jan 2016
v=find(A==0) A(v)=[]
Guillaume
on 18 Jan 2016
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
on 18 Jan 2016
Accepted Answer
More Answers (1)
shubhashree bal
on 14 Oct 2021
0 votes
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.';
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!