美文网首页
Matlab中如何扩展矩阵,其他部分补零?

Matlab中如何扩展矩阵,其他部分补零?

作者: 明明就_faf8 | 来源:发表于2019-05-30 11:10 被阅读0次

一、使用官方命令 blkdiag

在matlab中搜索:help blkdiag
下面是其官方事例:
Construct block diagonal matrix from input arguments

Syntax

out = blkdiag(a,b,c,d,...)

Description

out = blkdiag(a,b,c,d,...), where a, b, c, d, ... are matrices, outputs a block diagonal matrix of the form

The input matrices do not have to be square, nor do they have to be of equal size.即:输入的矩阵可以不是方阵
例如:

blkdiag (rand(2),ones(3))

ans =

0.9649    0.9706         0         0         0
0.1576    0.9572         0         0         0
     0         0    1.0000    1.0000    1.0000
     0         0    1.0000    1.0000    1.0000
     0         0    1.0000    1.0000    1.0000

二、使用其他命令

目的:在B的后面补零
例如:A=[1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1]
B= [2 2
2 2]
补零之后变成:
B=[2 2 0 0
2 2 0 0
0 0 0 0
0 0 0 0]

在matlab命令框中输入以下命令:

A=[1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1]

B=[2 2
2 2]

A =

 1     1     1     1
 1     1     1     1
 1     1     1     1
 1     1     1     1

B =

 2     2
 2     2

C=zeros(4,4)

C =

 0     0     0     0
 0     0     0     0
 0     0     0     0
 0     0     0     0

C(1:2,1:2)=B

C =

 2     2     0     0
 2     2     0     0
 0     0     0     0
 0     0     0     0

B=C

B =

 2     2     0     0
 2     2     0     0
 0     0     0     0
 0     0     0     0

更简单的一条指令:

B=[2 2
2 2]

B =

 2     2
 2     2

B(4,4)=0

B =

 2     2     0     0
 2     2     0     0
 0     0     0     0
 0     0     0     0

相关文章

网友评论

      本文标题:Matlab中如何扩展矩阵,其他部分补零?

      本文链接:https://www.haomeiwen.com/subject/wnjmtctx.html