如何在MATLAB中基于矢量创建三角矩阵?

时间:2022-01-16 13:05:38

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...)

(我在MATLAB中非常新手,但我一直背负着这些东西...)

4 个解决方案

#1


hankel(A) will get you the first matrix

汉克尔(A)将为您提供第一个矩阵

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

triu(toeplitz(A))将为您提供第二个。

--Loren

#2


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

Loren列出了最佳解决方案。也可以使用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. =)

我记得在我发现Loren提到的一些内置函数之前创建这样的带状矩阵。它并不像使用它那么简单和干净,但它起作用了。 =)

#3


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

我要做的就是创建一个矩阵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.

然后找到一个矩阵B,这样当你乘以A * B时,你就会得到你想要的结果。首先在纸上做线性代数然后让Matlab进行计算。

#4


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

为了产生具有这种规则图案的这种三角矩阵,使用例如toeplitz函数。

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

for the other case, use rot90(m)

对于另一种情况,使用rot90(m)

#1


hankel(A) will get you the first matrix

汉克尔(A)将为您提供第一个矩阵

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

triu(toeplitz(A))将为您提供第二个。

--Loren

#2


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

Loren列出了最佳解决方案。也可以使用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. =)

我记得在我发现Loren提到的一些内置函数之前创建这样的带状矩阵。它并不像使用它那么简单和干净,但它起作用了。 =)

#3


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

我要做的就是创建一个矩阵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.

然后找到一个矩阵B,这样当你乘以A * B时,你就会得到你想要的结果。首先在纸上做线性代数然后让Matlab进行计算。

#4


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

为了产生具有这种规则图案的这种三角矩阵,使用例如toeplitz函数。

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

for the other case, use rot90(m)

对于另一种情况,使用rot90(m)