NxN matrix with 2s on main diagonal

10 Ansichten (letzte 30 Tage)
ieva ashmore
ieva ashmore am 30 Nov. 2016
Beantwortet: Andrei Bobrov am 30 Nov. 2016
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.
  1 Kommentar
KSSV
KSSV am 30 Nov. 2016
You can create using diag. Doc diag.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 30 Nov. 2016
N = 8; %e.g.
m = full(gallery('tridiag', N));
m([N, (N-1)*N+1]) = -1
  3 Kommentare
ieva ashmore
ieva ashmore am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by