Create a Matrix with a specific main diagonal

I want to create a matrix of all one's with the main diagonal of value n.

 채택된 답변

Thorsten
Thorsten 2015년 11월 30일

1 개 추천

val = 50; N = 10;
X = diag(repmat(val-1, 1, 10)) + ones(N)

추가 답변 (3개)

dpb
dpb 2015년 11월 30일

0 개 추천

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일

0 개 추천

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일

0 개 추천

d = randi(35,5,1);
out = ~eye(5) + diag(d);

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2015년 11월 30일

편집:

2015년 12월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by