Put elements into corresponding locations of upper triangular matrix
Ältere Kommentare anzeigen
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 Kommentar
Jan
am 26 Jul. 2017
Is this a homework question? If so, please mention it, because then a different type of answers is required.
Akzeptierte Antwort
Weitere Antworten (1)
Roger Stafford
am 26 Jul. 2017
Let vector ‘inpt’ have size = n*(n+1)/2,1.
otpt = zeros(n);
otpt(triu(ones(n),0)==1) = inpt;
3 Kommentare
Prabhjot Dhami
am 23 Apr. 2020
Thanks for this!
warnerchang
am 4 Jun. 2021
Brilliant! it's actually the sum formula for arithmetic sequence! very helpful for understanding.
KUMAR TRIPATHY
am 3 Okt. 2021
Absolutely brilliant, concise and crisp!
Kategorien
Mehr zu Surrogate Optimization finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!