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

4 次查看(过去 30 天)
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]

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by