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

4 visualizaciones (últimos 30 días)
Alicia Gillies
Alicia Gillies el 11 de Feb. de 2018
Respondida: Walter Roberson el 11 de Feb. de 2018
I know you could simply use the Diag function but I am playing around with making scripts and I am curious.

Respuestas (2)

Star Strider
Star Strider el 11 de Feb. de 2018
Editada: Star Strider el 11 de Feb. de 2018
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 el 11 de Feb. de 2018
diagM = @(M) M(1:size(M,1)+1:size(M,1)*min(size(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