MATLAB: failed creating a diagonal matrix

2 ビュー (過去 30 日間)
Dastageer Khan
Dastageer Khan 2021 年 11 月 11 日
編集済み: James Tursa 2021 年 11 月 11 日
I have to make a diagonal matrix using diag command in Matlab, the numbers of the main diagonal are 1,2 and 3 . I have tried typing this code:
x=[1 2 3]; matriz=diag(x)
matriz =
1 2 3
but there is not the diagonal 3x3 matriz, please, could you help me solving this problem? Thanks! PS. As far as I'm concerned, it is supposed to show this:
B=[1 0 0;0 2 0;0 0 3]
B =
1 0 0
0 2 0
0 0 3

回答 (1 件)

James Tursa
James Tursa 2021 年 11 月 11 日
編集済み: James Tursa 2021 年 11 月 11 日
You probably have inadvertently created a variable named 'diag' in your workspace, and hence diag(x) is simply indexing into this variable. Clear that variable from your workspace and use a different name in the future. To confirm that this is the problem you can type the following:
which diag

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by