i want to make identity matrix

3 visualizaciones (últimos 30 días)
Qonitat
Qonitat el 4 de Mzo. de 2023
Editada: John D'Errico el 4 de Mzo. de 2023
i want to make a matrix that look like this
How could i achieve this efficiently .Thanks in advace

Respuestas (1)

John D'Errico
John D'Errico el 4 de Mzo. de 2023
Editada: John D'Errico el 4 de Mzo. de 2023
For only 3 blocks, it is simplest just to do this.
A = blkdiag([1 1],[1 1],[1 1])
A = 3×6
1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
Or, you could do this, if you have more than just 3 blocks. So for 5 blocks, you might do this:
A = kron(eye(5),[1 1])
A = 5×10
1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1
For many blocks, I would suggest creating a sparse matrix. A simple way of doing that would be to gain use kron, but make one of the pieces sparse.
A = kron(speye(1000),[1 1]);
As you can see, A is a sparse matrix now, so it requires much lles space to store, and many computations using this sparse matrix will be faster.
whos A
Name Size Bytes Class Attributes A 1000x2000 48008 double sparse
spy(A)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by