Creating a Matrix with a pattern

1 Ansicht (letzte 30 Tage)
James Wilson
James Wilson am 27 Mai 2021
Beantwortet: Walter Roberson am 27 Mai 2021
I am trying to create a pattern that starts to follow a pattern starting at the second row in the first column, then on the third row but starts on the second column and continues until an n term, creating a [n;n] matrix. The pattern is similar to this matrix:
A = [ 2,-2,0,0,0,0,0,0,0,0,0; -2,4,-2,0,0,0,0,0,0,0,0; 0,-2,4,-2,0,0,0,0,0,0,0; 0,0,-2,4,-2,0,0,0,0,0,0;
0,0,0,-2,4,-2,0,0,0,0,0; 0,0,0,0,-2,4,-2,0,0,0,0; 0,0,0,0,0,-2,4,-2,0,0,0; 0,0,0,0,0,0,-2,4,-2,0,0;
0,0,0,0,0,0,0,-2,4,-2,0; 0,0,0,0,0,0,0,0,-2,4,-2; 0,0,0,0,0,0,0,0,0,-2,4]
I am wondering how I can replicate the pattern above in a similar code then typing it out.
  1 Kommentar
Walter Roberson
Walter Roberson am 27 Mai 2021
A = [ 2,-2,0,0,0,0,0,0,0,0,0; -2,4,-2,0,0,0,0,0,0,0,0; 0,-2,4,-2,0,0,0,0,0,0,0; 0,0,-2,4,-2,0,0,0,0,0,0;
0,0,0,-2,4,-2,0,0,0,0,0; 0,0,0,0,-2,4,-2,0,0,0,0; 0,0,0,0,0,-2,4,-2,0,0,0; 0,0,0,0,0,0,-2,4,-2,0,0;
0,0,0,0,0,0,0,-2,4,-2,0; 0,0,0,0,0,0,0,0,-2,4,-2; 0,0,0,0,0,0,0,0,0,-2,4]
A = 11×11
2 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Mai 2021
N = 11;
diag([2, 4*ones(1,N-2), 2]) + diag(-2*ones(1,N-1),1) + diag(-2*ones(1,N-1),-1)
ans = 11×11
2 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2 0 0 0 0 0 0 0 0 0 -2 4 -2

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping 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