How to write code for diagonal matrix with specified input ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ugashini Preetha
am 27 Mai 2022
Kommentiert: Ugashini Preetha
am 30 Mai 2022
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 Kommentare
Walter Roberson
am 27 Mai 2022
We recommend that you do not dynamically create variable names such as a93829382.
Akzeptierte Antwort
KSSV
am 27 Mai 2022
n = 3 ;
s = 2 ;
iwant = zeros(n) ;
for i = 1:s
iwant(i,i)=1 ;
end
iwant
5 Kommentare
Walter Roberson
am 30 Mai 2022
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
Weitere Antworten (1)
kainat rasheed
am 27 Mai 2022
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!