Create a Matrix with a specific main diagonal

13 ビュー (過去 30 日間)
Adzlxix
Adzlxix 2015 年 11 月 30 日
編集済み: Adzlxix 2015 年 12 月 12 日
I want to create a matrix of all one's with the main diagonal of value n.

採用された回答

Thorsten
Thorsten 2015 年 11 月 30 日
val = 50; N = 10;
X = diag(repmat(val-1, 1, 10)) + ones(N)

その他の回答 (3 件)

dpb
dpb 2015 年 11 月 30 日
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 2015 年 11 月 30 日
編集済み: Stephen23 2015 年 11 月 30 日
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 2015 年 11 月 30 日
d = randi(35,5,1);
out = ~eye(5) + diag(d);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by