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

1 次查看(过去 30 天)
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