NxN matrix with 2s on main diagonal
10 views (last 30 days)
Show older comments
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
on 30 Nov 2016
N = 8; %e.g.
m = full(gallery('tridiag', N));
m([N, (N-1)*N+1]) = -1
3 Comments
Sean de Wolski
on 30 Nov 2016
He's using linear indexing. https://www.mathworks.com/help/releases/R2016b/matlab/math/matrix-indexing.html
More Answers (1)
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!