필터 지우기
필터 지우기

How to write code for diagonal matrix with specified input ?

조회 수: 1 (최근 30일)
Ugashini Preetha
Ugashini Preetha 2022년 5월 27일
댓글: Ugashini Preetha 2022년 5월 30일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2022년 5월 27일
n = 3 ;
s = 2 ;
iwant = zeros(n) ;
for i = 1:s
iwant(i,i)=1 ;
end
iwant
  댓글 수: 5
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

kainat rasheed
kainat rasheed 2022년 5월 27일
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

카테고리

Help CenterFile 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