Is possible put a vector as a diagonal of matrix ?

2 vues (au cours des 30 derniers jours)
Alex
Alex le 13 Fév 2014
Commenté : Jos (10584) le 13 Fév 2014
I mean, if a have:
A= [1 2];
can i get
B=
[1 2 0 0 0 0;
0 0 1 2 0 0;
0 0 0 0 1 2];
without any loop ?

Réponses (3)

Andrei Bobrov
Andrei Bobrov le 13 Fév 2014
Modifié(e) : Andrei Bobrov le 13 Fév 2014
blkdiag(A,A,A)
or
A = [1 2];
n = 4;
a1 = repmat({A},n,1);
out = blkdiag(a1{:});

Azzi Abdelmalek
Azzi Abdelmalek le 13 Fév 2014
n=3;
a=repmat([1 2 zeros(1,2*n-2)],n,1);
b=arrayfun(@(x) circshift(a(x,:),[0 2*(x-1)]),(1:n)','un',0);
out=cell2mat(b)

Jos (10584)
Jos (10584) le 13 Fév 2014
A = [1 2] ;
n = 3 ;
B = kron(eye(n),A)
  2 commentaires
Alex
Alex le 13 Fév 2014
Thats works just fine !! thanks
Jos (10584)
Jos (10584) le 13 Fév 2014
You're welcome. If you're satisfied you can accept the answer so others know as well.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays 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