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.
0 个评论
回答(2 个)
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(:);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!