Create a Matrix with a specific main diagonal

 Respuesta aceptada

Thorsten
Thorsten el 30 de Nov. de 2015
val = 50; N = 10;
X = diag(repmat(val-1, 1, 10)) + ones(N)

Más respuestas (3)

dpb
dpb el 30 de Nov. de 2015
Multiple ways possible; one--
>> N=5; % diagonal value
>> sz=3; % size
>> m=ones(sz)+diag(repmat(N-1,1,sz))
m =
5 1 1
1 5 1
1 1 5
>>
doc diag % for details on optional arguments for different resulting types
Stephen23
Stephen23 el 30 de Nov. de 2015
Editada: Stephen23 el 30 de Nov. de 2015
One very simple solution:
>> X = +~eye(4);
>> X(X==0) = 5
X =
5 1 1 1
1 5 1 1
1 1 5 1
1 1 1 5

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Nov. de 2015

Editada:

el 12 de Dic. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by