For n*n diagonal matrix :
n = input (' enter the order of the matrix : ');
s = input (' enter the value of s : ');
My question is if m = 3 and s = 1 then I should get my matrix as [a11] = 1 and remaining entries 0
if m = 3 and s = 2 then [a11] = 1 , [a22] = 1 and remaining entries o.
please help me out with this :)

2 件のコメント

Walter Roberson
Walter Roberson 2022 年 5 月 27 日
We recommend that you do not dynamically create variable names such as a93829382.
Walter Roberson
Walter Roberson 2022 年 5 月 27 日
a(i, i) = 1

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

 採用された回答

KSSV
KSSV 2022 年 5 月 27 日

1 投票

n = 3 ;
s = 2 ;
iwant = zeros(n) ;
for i = 1:s
iwant(i,i)=1 ;
end
iwant

5 件のコメント

Ugashini Preetha
Ugashini Preetha 2022 年 5 月 27 日
Thankyou !!
KSSV
KSSV 2022 年 5 月 27 日
Note that loop can be avoided.
Ugashini Preetha
Ugashini Preetha 2022 年 5 月 30 日
I have another doubt sir,
For n*n diagonal matrix :
n = input (' enter the order of the matrix : ');
s = input (' enter the value of s : ');
I want to display my 1st (1,1) entry as "n" and the remaining entries of the diagonal as "n-1"
Is it possible to use the same for loop or I have to use if else statement ?
Walter Roberson
Walter Roberson 2022 年 5 月 30 日
You could initialize (1,1) and loop from 2
You could loop from 1 but change (1,1) after
You could loop and have an if to figure out what to do
Ugashini Preetha
Ugashini Preetha 2022 年 5 月 30 日
okay sir

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

その他の回答 (1 件)

kainat rasheed
kainat rasheed 2022 年 5 月 27 日

0 投票

v = [1 2 3 4 5];
use this matrix to use specific numbers like this
after that write it as
D = diag(v)
v = [1 2 3 4 5];
D = diag(v)
run the code

カテゴリ

ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by