Create a Matrix with a specific main diagonal
Mostra commenti meno recenti
I want to create a matrix of all one's with the main diagonal of value n.
Risposta accettata
Più risposte (3)
dpb
il 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
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
il 30 Nov 2015
d = randi(35,5,1);
out = ~eye(5) + diag(d);
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!