What is a function that takes the diagonal of a matrix (N) and converts the diagonal to all zeros.

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Youssef  Khmou
Youssef Khmou el 9 de Abr. de 2013
try :
function Y=diagzero(X)
N=size(X);
if N(1)~=N(2)
error(' Matrix not square');
end
Y=X;
for x=1:N(1)
Y(x,x)=0;
end

Más respuestas (4)

James Tursa
James Tursa el 10 de Abr. de 2013
Editada: James Tursa el 10 de Abr. de 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
Andrei Bobrov el 10 de Abr. de 2013
m(eye(size(m))>0) = 0;

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Abr. de 2013
Editada: Azzi Abdelmalek el 9 de Abr. de 2013
ii=1:size(m,1);
m(sub2ind(size(m),ii,ii))=0

Jonathan Epperl
Jonathan Epperl el 10 de Abr. de 2013
Editada: Jonathan Epperl el 10 de Abr. de 2013
Assuming m is square (because else you'd have to define the diagonal for me):
diagzero = @(m) m - diag(diag(m));

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by