How to create a 3 diagonal matrix with blocks

4 views (last 30 days)
I would like to create a matrix of the following form, ideally without having to use a loop as its size can become rather large:
basically it is an 2N x 2N matrix with a double wide 3 diagonal.
If anyone can provide some assistance in this it would be much appreciated.

Accepted Answer

Voss
Voss on 26 Jun 2022
Edited: Voss on 26 Jun 2022
N = 7;
M = zeros(2*N,N+2);
M( 1+(2*N+2)*(0:N-1) + reshape([0 1]+[0;1;2]*2*N,[],1) ) = 1;
disp(M);
1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!