Is possible put a vector as a diagonal of matrix ?
1 次查看(过去 30 天)
显示 更早的评论
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 ?
0 个评论
回答(3 个)
Andrei Bobrov
2014-2-13
编辑:Andrei Bobrov
2014-2-13
blkdiag(A,A,A)
or
A = [1 2];
n = 4;
a1 = repmat({A},n,1);
out = blkdiag(a1{:});
0 个评论
Azzi Abdelmalek
2014-2-13
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)
0 个评论
Jos (10584)
2014-2-13
A = [1 2] ;
n = 3 ;
B = kron(eye(n),A)
2 个评论
Jos (10584)
2014-2-13
You're welcome. If you're satisfied you can accept the answer so others know as well.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!