How to create a semi-diagonal matrix

조회 수: 5 (최근 30일)
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 11월 21일
댓글: Konstantinos Tsitsilonis 2017년 11월 22일
Hi all,
I have been trying to create a semi-diagonal matrix; Now I know this may not be the right terminology for it, however here it is (also, the matrix doesnt have to be square. I want to have control over its m x n dimensions. The example is thus of a semi-diagonal 3 x 6 matrix):
M_3x6 = [1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1] ;
Or for a larger matrix:
M_4x7 = [1 1 0 0 0 0 0
0 0 1 1 0 0 0
0 0 0 0 1 1 0
0 0 0 0 0 0 1] ;
Thanks for your help in advance,
KMT.

채택된 답변

Stephen23
Stephen23 2017년 11월 21일
편집: Stephen23 2017년 11월 21일
Method one: blkdiag:
>> blkdiag([1,1],[1,1],[1,1])
ans =
1 1 0 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
>>
You can easily supply the inputs as a comma-separated list:
>> C = repmat({[1,1]},1,4);
>> blkdiag(C{:})
ans =
1 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1
>>
Then use indexing to select a part of the matrix.
Method two: repelem: With newer MATLAB versions you could use eye and repelem, and then use indexing as above.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by