Borrar filtros
Borrar filtros

Is there any way to put a small matrix diagonally into a larger one?

4 visualizaciones (últimos 30 días)
ex: [1 2;3 4]
Big matrix :[1 2 0 0 0 0; 3 4 0 0 0 0; 0 0 1 2 0 0 0 0 3 4 0 0]

Respuesta aceptada

Chad Greene
Chad Greene el 4 de Abr. de 2016
A = [1 2; 3 4];
B = zeros(4,6);
B(1:2,1:2) = A;
B(3:4,3:4) = A;
  2 comentarios
nafila aytija
nafila aytija el 4 de Abr. de 2016
thank you for your answer.but if I want to put a [2,2] matrix into a [200,200] matrix, how do I do that without using ''blkdiag?
Steven Lord
Steven Lord el 4 de Abr. de 2016
Create the matrix to be replicated on the diagonal
A = [1 2; 3 4];
Create a cell array with copies
C = repmat({A}, 1, 100);
Use the cell array to generate a comma-separated list and pass that list into blkdiag as 100 inputs
M = blkdiag(C{:});
Show the upper-left corner to see that it was created as you want.
M(1:10, 1:10)
Or in this case, since you want the same matrix to be repeated, an easier approach that doesn't require calling blkdiag with 100 inputs uses kron instead. It gives the same result as the blkdiag call as you can see from the result from isequal.
K = kron(eye(100), A);
isequal(K, M)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal 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