Insert a matrix within a matrix

9 次查看(过去 30 天)
Im trying to insert a matrix within a matrix, such that, if
x=[a,d;c,d], then y=[a,b,0,0;c,d,0,0;0,0,a,b;0,0,c,d]. Basically, matrix X becomes the diagonals of of the zero matrix Y.
anyone in cyberland have an idea on how to do this?

采纳的回答

the cyclist
the cyclist 2011-1-29
Matt's solution does precisely what you ask. You might also like to know that in general, you can insert one array into another by indexing on the left-hand side of an assignment statement:
>> x = rand(2);
>> y = zeros(8);
>> y(5:6,3:4) = x;

更多回答(2 个)

Matt Fig
Matt Fig 2011-1-29
Assuming you meant
x = [a,b;c,d];
then
y = blkdiag(x,x)

PUST rahman
PUST rahman 2012-6-19
function R=insertMatrix(B,b)
% INPUT: B: Bigger matrix % b: small matrix that needs to put inside bigger matrix, B %OUTPUT: R: Resultant matrix % Example: % B=zeros(10,10); b=ones(5,5); % R=insertMatrix(B,b);
% this matlab script is written by Hafiz, PhD Student, UNSW, Canberra % hafizurcse@yahoo.com
[P,Q]=size(B);
fx=floor(P/2)-floor(size(b,1)/2);
fy=floor(Q/2)-floor(size(b,2)/2);
R=B;
for p=1:size(b,1)
for q=1:size(b,2)
R(fx+p,fy+q)=b(p,q);
end
end
return;
  1 个评论
PUST rahman
PUST rahman 2012-6-19
The only advantage of this code is it tries to push the small matrix just in the middle of the bigger matrix.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by