Put elements into corresponding locations of upper triangular matrix
이전 댓글 표시
Hi all,
Imagine I have a vector:
inpt = (1:6)';
Now I'd like to put elements of inpt in the upper triangular part of a 3 by 3 matrix otpt, so I have:
otpt =
1 2 4
0 3 5
0 0 6
What's the best way to do it? Thanks!
댓글 수: 1
Jan
2017년 7월 26일
Is this a homework question? If so, please mention it, because then a different type of answers is required.
채택된 답변
추가 답변 (1개)
Roger Stafford
2017년 7월 26일
Let vector ‘inpt’ have size = n*(n+1)/2,1.
otpt = zeros(n);
otpt(triu(ones(n),0)==1) = inpt;
댓글 수: 3
Prabhjot Dhami
2020년 4월 23일
Thanks for this!
warnerchang
2021년 6월 4일
Brilliant! it's actually the sum formula for arithmetic sequence! very helpful for understanding.
KUMAR TRIPATHY
2021년 10월 3일
Absolutely brilliant, concise and crisp!
카테고리
도움말 센터 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!