建立特殊的矩阵

clear
>> Random = rand(2 , 3)

Random =

0.9572 0.8003 0.4218
0.4854 0.1419 0.9157

>> Array = Random(: , 2)

Array =

0.8003
0.1419

>> Diagelement = diag(Random)

Diagelement =

0.9572
0.1419

>> Diagmatrix = diag(diag(Random))

Diagmatrix =

0.9572 0
0 0.1419

>> Dmatrix_array = diag(Array)

Dmatrix_array =

0.8003 0
0 0.1419

>> UpperTriangular = triu(Random)

UpperTriangular =

0.9572 0.8003 0.4218
0 0.1419 0.9157

>> lowerTriangular = tril(Random)

lowerTriangular =

0.9572 0 0
0.4854 0.1419 0

 

clear
>> x = 10 + 2i
shape = size(x)
y = [10 + 2i] %将复数10+2i构成的矩阵赋值给变量y
shape = size(y)
x == y

x =

10.0000 + 2.0000i


shape =

1 1


y =

10.0000 + 2.0000i


shape =

1 1


ans =

1

说明:MATLAB中单个实数或复数都是以矩阵的形式存储的。

 

%矩阵大小及结构的改变
>> Randoma = randn ( 1 , 4 )%生成1*4的随机矩阵
Randomb = randn (2) %生成随机2*2矩阵
Randoma = reshape (Randoma , 2 , 2)%将1*4矩阵变为2*2矩阵
Randoma = fliplr (Randoma)%将2*2矩阵每行逆序排列
Randoma = rot90(Randoma)%将2*2矩阵逆时针旋转90度
Randomc = cat (2 , Randoma , Randomb)%将两个2*2矩阵组合成为一个2*4矩阵

Randoma =

0.5377 1.8339 -2.2588 0.8622


Randomb =

0.3188 -0.4336
-1.3077 0.3426


Randoma =

0.5377 -2.2588
1.8339 0.8622


Randoma =

-2.2588 0.5377
0.8622 1.8339


Randoma =

0.5377 1.8339
-2.2588 0.8622


Randomc =

0.5377 1.8339 0.3188 -0.4336
-2.2588 0.8622 -1.3077 0.3426

%矩阵下标的引用

Matrix = magic(6) %构建n*n的矩阵,每一行,每一列元素之和相等
Submatrix = Matrix( 2: 3 , 3 : 6 )
Array = Matrix( [ 7 : 10 26 : 31] )

Matrix =

35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11


Submatrix =

7 21 23 25
2 22 27 20


Array =

1 32 9 28 23 27 10 14 18 24

 

原文地址:https://www.cnblogs.com/2017-6-15/p/7801155.html