hi, i would like to get some help with defining the next matrix in the easiest way. i know the diag() function but i didnt find a way the define the non-main diagonals.

 Réponse acceptée

N = 7;
M = toeplitz([-2,1,zeros(1,N-2)])
M = 7×7
-2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2 1 0 0 0 0 0 1 -2

2 commentaires

Nitzan Haviv
Nitzan Haviv le 4 Août 2021
thanks a lot!
Stephen23
Stephen23 le 4 Août 2021
@Nitzan Haviv: my pleasure! Remember to click the accept button if my answer helped you!

Connectez-vous pour commenter.

Plus de réponses (1)

Nitzan Haviv
Nitzan Haviv le 4 Août 2021
btw i found another way
when N is the dimention of the square matrix
d_sec = ones(1,N-1);
d_main= ones(1,N).*-2;
H = diag(d_main)+diag(d_sec,-1) + diag(d_sec,1);

Catégories

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by