Square Matris (diagonal)

5 ビュー (過去 30 日間)
ukulele
ukulele 2021 年 3 月 22 日
編集済み: Jan 2021 年 3 月 24 日
With the Nested For loop, the diagonals line number according to the value entered by the user.
I want to write a code that creates a square matrix as many as the number that the user enters, and the other elements that show are zero.Like this if users input 4, i want to see this
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4

回答 (1 件)

KSSV
KSSV 2021 年 3 月 22 日
編集済み: Walter Roberson 2021 年 3 月 22 日
n = 4 ;
iwant = diag(1:n)
iwant = 4×4
1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4
with loop:
n = 4 ;
iwant = zeros(n) ;
for i = 1:n
iwant(i,i) = i ;
end
iwant
iwant = 4×4
1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4
  9 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 22 日
編集済み: Walter Roberson 2021 年 3 月 22 日
n=input('Bir sayi giriniz=');
k=diag(1:n) %notice semi-colon removed
for i=1:n
k(n,n)=n %notice semi-colon removed
end
A statement that ends with a semi-colon does not display its value.
A statement that does not end with a semi-colon always displays its value if it was an assignment statement; if it was not an assignement statement, then in some cases it will show the value and in other cases it will not show the value, depending exactly what you were asking for.
ukulele
ukulele 2021 年 3 月 22 日
i get it thank youuu

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by