Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
nafila aytija
nafila aytija le 4 Avr 2016
Commenté : Steven Lord le 4 Avr 2016
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]

Réponse acceptée

Chad Greene
Chad Greene le 4 Avr 2016
A = [1 2; 3 4];
B = zeros(4,6);
B(1:2,1:2) = A;
B(3:4,3:4) = A;
  2 commentaires
nafila aytija
nafila aytija le 4 Avr 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 le 4 Avr 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)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by