What is a function that takes the diagonal of a matrix (N) and converts the diagonal to all zeros.
Ältere Kommentare anzeigen
Ex.
>> m=[1 2 3; 4 5 6;1 2 3]
m =
1 2 3
4 5 6
1 2 3
>> m=diagzero (m)
m =
0 2 3
4 0 6
1 2 0
Akzeptierte Antwort
Weitere Antworten (4)
James Tursa
am 10 Apr. 2013
Bearbeitet: James Tursa
am 10 Apr. 2013
Another method if you know m is square:
r = size(m,1) + 1;
m(1:r:end) = 0;
If you don't know if m is square or not you could do this:
r = size(m,1);
n = min(numel(m),r*r);
m(1:r+1:n) = 0;
Andrei Bobrov
am 10 Apr. 2013
m(eye(size(m))>0) = 0;
1 Kommentar
James Tursa
am 10 Apr. 2013
Or to avoid testing all those 0's:
m(speye(size(m))>0) = 0;
or:
m(logical(speye(size(m)))) = 0;
Azzi Abdelmalek
am 9 Apr. 2013
Bearbeitet: Azzi Abdelmalek
am 9 Apr. 2013
ii=1:size(m,1);
m(sub2ind(size(m),ii,ii))=0
Jonathan Epperl
am 10 Apr. 2013
Bearbeitet: Jonathan Epperl
am 10 Apr. 2013
Assuming m is square (because else you'd have to define the diagonal for me):
diagzero = @(m) m - diag(diag(m));
1 Kommentar
James Tursa
am 10 Apr. 2013
Doesn't work on any diagonal elements that are inf or nan.
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!