My symmetric matrix should look like
[1 0 0 0 0..n no. of zeroes;
1 -2 1 0 0 0..n;
0 1 -2 1 0 0 ..;
for n no of rows]

1 comentario

James Tursa
James Tursa el 13 de Nov. de 2015
Editada: James Tursa el 13 de Nov. de 2015
Are the last two rows this?
0 ... 0 1 -2 1;
0 ... 0 0 0 1];
Or this?
0 ... 0 1 -2 1;
0 ... 0 0 1 -2];
Is this an n X n matrix or an n X n+1 matrix? It is not clear from your example what the matrix should really look like since your example is not symmetric (e.g., the (2,1) spot is not equal to the (1,2) spot)

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 13 de Nov. de 2015
Editada: James Tursa el 13 de Nov. de 2015
Making a guess at what you really want, you could build it in pieces, e.g.,
n = size of matrix;
A = zeros(n);
A(1:n+1:end) = -2; % The diagonal
A(2:n+1:end) = 1; % The lower diagonal
A(n+1:n+1:end) = 1; % The upper diagonal
A(1,1) = 1; % Different value for the (1,1) spot.
An alternative method could employ the spdiags function to build the matrix.

2 comentarios

Nitesh Panchal
Nitesh Panchal el 13 de Nov. de 2015
Few twitches and it worked perfectly thank you
Stephen23
Stephen23 el 14 de Nov. de 2015
See my answer for a much simpler way to create this matrix.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 14 de Nov. de 2015
Editada: Stephen23 el 14 de Nov. de 2015
Simply use toeplitz:
>> n = 6;
>> M = toeplitz([-2,1,zeros(1,n-2)]);
>> M(1) = 1; M(end) = 1
M =
1 1 0 0 0 0
1 -2 1 0 0 0
0 1 -2 1 0 0
0 0 1 -2 1 0
0 0 0 1 -2 1
0 0 0 0 1 1

1 comentario

Nitesh Panchal
Nitesh Panchal el 14 de Nov. de 2015
It was pretty simple but harder to comprehend , i tried using this command but i couldnt figure it out , but thank you so much

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Nov. de 2015

Comentada:

el 14 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by