Creating a matrix using diag

Hi,
Would you please help me to write a script with "diag" command to create the matrix as shown in image. http://s1.postimage.org/5i4gd1esv/matrix.jpg By they way, it's a 99 X 99 matrix.
Thank you, John

댓글 수: 2

Matt J
Matt J 2013년 1월 27일
편집: Matt J 2013년 1월 27일
The toeplitz() command would be quicker.
John Edward
John Edward 2013년 1월 27일
Thanks for your reply and I forgot to mention that it's a 99 by 99 matrix. Can you be able to script it for me.
Thank you, John

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

 채택된 답변

Matt J
Matt J 2013년 1월 27일

1 개 추천

>> z=zeros(3,1); z(1)=2; z(2)=-1; A=toeplitz(z)
A =
2 -1 0
-1 2 -1
0 -1 2

댓글 수: 3

John Edward
John Edward 2013년 1월 27일
Matt, how would you make it a 99 X 99 matrix. That's where I am stock. Thx man.
Cedric
Cedric 2013년 1월 27일
편집: Cedric 2013년 1월 27일
Look at what happens if you replace the 3 in Matt's answer with e.g. 5.
But just to be sure, you want a tri-diagonal 99x99 matrix, right?
John Edward
John Edward 2013년 1월 27일
Thank YOU, YOU are a life saver. It worked.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2013년 1월 27일

0 개 추천

diag(this, -1) + diag(that) + diag(theother,1)

댓글 수: 1

John Edward
John Edward 2013년 1월 27일
Thanks for your reply but it's a 99 by 99 matrix.

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

Matt J
Matt J 2013년 1월 27일
편집: Matt J 2013년 1월 27일

0 개 추천

A small example
>> A=diag([1,1,1])-diag([1,1],1); A=A+A.'
A =
2 -1 0
-1 2 -1
0 -1 2

댓글 수: 4

John Edward
John Edward 2013년 1월 27일
Thanks for your reply but it's a 99 by 99 matrix. How would you script that? Thx a lot.
Matt J
Matt J 2013년 1월 27일
If you understood how it works for 3x3, it should be pretty obvious how to extend it to the 99x99 case.
John Edward
John Edward 2013년 1월 27일
Honestly, I am new to matlab and I have been working on this for past 4 hours and I can't get the code right. Can you help me on that? Thank you.
I don't know what more help I can give without just doing the whole thing for you. Are you aware that
>> help diag
or
>> doc diag
will display an explanation of the diag command? If you've read that documentation, it should be really easy. Some more examples:
>> diag([1 2 3 4]), diag([10 20 30], 2), diag([5 7 6],-1)
ans =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
ans =
0 0 10 0 0
0 0 0 20 0
0 0 0 0 30
0 0 0 0 0
0 0 0 0 0
ans =
0 0 0 0
5 0 0 0
0 7 0 0
0 0 6 0

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

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2013년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by