How to create a 3 diagonal matrix with blocks

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.

 Réponse acceptée

Voss
Voss le 26 Juin 2022
Modifié(e) : Voss le 26 Juin 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

2 commentaires

Perfect, this should work wonders.
Thank you very much
Voss
Voss le 27 Juin 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by