Is possible put a vector as a diagonal of matrix ?

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 ?

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 13 日
編集済み: Andrei Bobrov 2014 年 2 月 13 日

0 投票

blkdiag(A,A,A)
or
A = [1 2];
n = 4;
a1 = repmat({A},n,1);
out = blkdiag(a1{:});
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 13 日

0 投票

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) 2014 年 2 月 13 日

0 投票

A = [1 2] ;
n = 3 ;
B = kron(eye(n),A)

2 件のコメント

Alex
Alex 2014 年 2 月 13 日
Thats works just fine !! thanks
Jos (10584)
Jos (10584) 2014 年 2 月 13 日
You're welcome. If you're satisfied you can accept the answer so others know as well.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

質問済み:

2014 年 2 月 13 日

コメント済み:

2014 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by