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에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!