How would I make a script to find the Diagonal of a matrix?

조회 수: 3 (최근 30일)
Alicia Gillies
Alicia Gillies 2018년 2월 11일
답변: Walter Roberson 2018년 2월 11일
I know you could simply use the Diag function but I am playing around with making scripts and I am curious.

답변 (2개)

Star Strider
Star Strider 2018년 2월 11일
편집: Star Strider 2018년 2월 11일
Hi, Curious Alicia Gillies!
I would do this:
M = ...; % Your Matrix
for k = 1:min(size(M))
diagM(k) = M(k,k);
end
and to create a diagonal matrix from the same ‘diagM’ (or any other) vector:
N = eye(numel(diagM)) .* diagM(:);

Walter Roberson
Walter Roberson 2018년 2월 11일
diagM = @(M) M(1:size(M,1)+1:size(M,1)*min(size(M))).';

카테고리

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