Create a Matrix with a specific main diagonal

8 vues (au cours des 30 derniers jours)
Adzlxix
Adzlxix le 30 Nov 2015
Modifié(e) : Adzlxix le 12 Déc 2015
I want to create a matrix of all one's with the main diagonal of value n.

Réponse acceptée

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

Plus de réponses (3)

dpb
dpb le 30 Nov 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 le 30 Nov 2015
Modifié(e) : Stephen23 le 30 Nov 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

Andrei Bobrov
Andrei Bobrov le 30 Nov 2015
d = randi(35,5,1);
out = ~eye(5) + diag(d);

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by