NxN matrix with 2s on main diagonal

10 views (last 30 days)
How do I create NxN matrix which has 2s on main diagonal, -1s on diagonal above and below 2s and -1 last element (Nth) in the first row and first element in Nth row.

Accepted Answer

Guillaume
Guillaume on 30 Nov 2016
N = 8; %e.g.
m = full(gallery('tridiag', N));
m([N, (N-1)*N+1]) = -1
  3 Comments
ieva ashmore
ieva ashmore on 30 Nov 2016
Guillaume, Just out of pure interest can you expand on m([N, (N-1)*N+1]) = -1 I see that it could have been done with two commands m(1,N)=-1 and m(N,1)=-1 But I would like to learn more about the method you have used Thanks

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 30 Nov 2016
toeplitz([2 -1 zeros(1,n-3) -1])

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!