How would I make a script to find the Diagonal of a matrix?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I know you could simply use the Diag function but I am playing around with making scripts and I am curious.
0 comentarios
Respuestas (2)
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(:);
0 comentarios
Walter Roberson
el 11 de Feb. de 2018
diagM = @(M) M(1:size(M,1)+1:size(M,1)*min(size(M))).';
0 comentarios
Ver también
Categorías
Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!