Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
正方行列 が与えられた場合M、対角要素の和をどのように求めることができますか? これよりも簡単な方法があるはずです:
M
sum(sum(diag(diag(M), 0)))
実際、私が探していたのはトレースでした:
1> M = reshape(1:9, 3, 3) M = 1 4 7 2 5 8 3 6 9 2> trace(M) ans = 15
うまくsum(diag(M))機能しているようです。
sum(diag(M))
1> M = reshape(1:9, 3, 3) M = 1 4 7 2 5 8 3 6 9 2> sum(diag(M)) ans = 15