How would I make a script to find the Diagonal of a matrix?
1 view (last 30 days)
Show older comments
I know you could simply use the Diag function but I am playing around with making scripts and I am curious.
0 Comments
Answers (2)
Star Strider
on 11 Feb 2018
Edited: Star Strider
on 11 Feb 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 Comments
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!