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

 채택된 답변

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

추가 답변 (4개)

James Tursa
James Tursa 2013년 4월 10일
편집: James Tursa 2013년 4월 10일
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;
Assuming m is square (because else you'd have to define the diagonal for me):
diagzero = @(m) m - diag(diag(m));

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2013년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by