How to create a 3 diagonal matrix with blocks

1 Ansicht (letzte 30 Tage)
Johan De Jager
Johan De Jager am 26 Jun. 2022
Kommentiert: Voss am 27 Jun. 2022
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.

Akzeptierte Antwort

Voss
Voss am 26 Jun. 2022
Bearbeitet: Voss am 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
  2 Kommentare
Johan De Jager
Johan De Jager am 27 Jun. 2022
Perfect, this should work wonders.
Thank you very much
Voss
Voss am 27 Jun. 2022
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by