Creating matrix with diag command

2 ビュー (過去 30 日間)
sky2
sky2 2021 年 3 月 21 日
編集済み: Stephan 2021 年 3 月 21 日
I am trying to do this
a = input('enter a number: ')
for i=1:1:a
for j=1:1:a
z(i,j)= diag(a)
end
end
z
I enter 10 to create 10x10 matrix. I am trying to do corner to corner 1-10 like diag command and do other numbers 0. What is my mistake? I must do this with nested for-end.

採用された回答

Stephan
Stephan 2021 年 3 月 21 日
編集済み: Stephan 2021 年 3 月 21 日
This is how a normal Matlab user would so this
z = diag(1:10)
z =
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0
0 0 0 0 0 6 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 8 0 0
0 0 0 0 0 0 0 0 9 0
0 0 0 0 0 0 0 0 0 10
However:
a = input('enter a number: ')
z = zeros(a);
for ii=1:1:a
for jj=1:1:a
if ii == jj
z(ii,jj)= ii;
end
end
end
disp(z)
  1 件のコメント
sky2
sky2 2021 年 3 月 21 日
編集済み: sky2 2021 年 3 月 21 日
Ok thanks for helping. I will try now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by