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

2 ビュー (過去 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 Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by