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

前端 未结 4 2224
渐次进展
渐次进展 2021-02-20 02:55

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

A = [101:105]

Which is really:

[ 101, 102, 103, 104, 105 ]

And I

相关标签:
4条回答
  • 2021-02-20 03:15

    For generating such triangular matrices with such a regular pattern, use the toeplitz function, e.g.

    m=toeplitz([1,0,0,0],[1,2,3,4])
    

    for the other case, use rot90(m)

    0 讨论(0)
  • 2021-02-20 03:19

    The best solutions are listed by Loren. It's also possible to create these matrices using SPDIAGS:

    vec = 101:105;
    A = full(spdiags(repmat(vec,5,1),0:4,5,5));  % The second matrix
    B = fliplr(full(spdiags(repmat(fliplr(vec),5,1),0:4,5,5)));  % The first matrix
    

    I recall creating banded matrices like this before I found out about some of the built-in functions Loren mentioned. It's not nearly as simple and clean as using those, but it worked. =)

    0 讨论(0)
  • 2021-02-20 03:20

    The way I'd go about it is to create a matrix A:

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

    And then find a matrix B such that when you multiply A*B you'll get the result you want. Basically do the linear algebra on paper first and then have Matlab do the calculation.

    0 讨论(0)
  • 2021-02-20 03:26

    hankel(A) will get you the first matrix

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

    --Loren

    0 讨论(0)
提交回复
热议问题