Insert a matrix within a matrix
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (2)
Matt Fig
le 29 Jan 2011
Assuming you meant
x = [a,b;c,d];
then
y = blkdiag(x,x)
PUST rahman
le 19 Juin 2012
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);
[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 commentaire
PUST rahman
le 19 Juin 2012
The only advantage of this code is it tries to push the small matrix just in the middle of the bigger matrix.
Catégories
En savoir plus sur Operating on Diagonal Matrices dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!