How can I create a triangular matrix based on a vector, in MATLAB?

Shalom Craimer picture Shalom Craimer · Jun 16, 2009 · Viewed 13.4k times · Source

Let's say I've got a vector like this one:

A = [101:105]

Which is really:

[ 101, 102, 103, 104, 105 ]

And I'd like to use only vector/matrix functions and operators to produces the matrix:

101 102 103 104 105
102 103 104 105 0
103 104 105 0   0
104 105 0   0   0
105 0   0   0   0

or the following matrix:

101 102 103 104 105
0   101 102 103 104
0   0   101 102 103
0   0   0   101 102
0   0   0   0   101

Any ideas anyone?

(I'm very much a novice in MATLAB, but I've been saddled this stuff...)

Answer

Loren picture Loren · Jun 16, 2009

hankel(A) will get you the first matrix

triu(toeplitz(A)) will get you the second one.

--Loren