MATLAB基础笔记1:数组矩阵操作

MATLAB笔记1: Array Operation

变量

  • ans 变量
sin(cos(pi))

等于

cos(pi)
sin(ans)
  • 其他变量
  1. i,j: complex number
  2. Inf: ∞
  3. eps: 2.2204e-016
  4. NaN: not a number
  5. pi: π
x = 1/0        # 结果为 Inf
x = log(0)     # 结果为 -Inf
x = inf/inf    # 结果为 NaN

数组

  • Array (Vector and Matrix) 向量和矩阵

Row Vector 行向量:

a = [1 2 3 4]

Column Vector 列向量:

a = [1; 2; 3; 4]

数组操作

  • Array Indexing 索引
>> A = [1 21 6;5 17 9; 31 2 7]

A =

     1    21     6
     5    17     9
    31     2     7

索引:

>> A(8)      # 列优先

ans =

     9

>> A([1 3 5])

ans =

     1    31    17

>> A([1 3;1 3])

ans =

     1    31
     1    31
>> A(3,2)

ans =

     2

>> A([1 3],[1 3])  # 第1,3行和第1,3列交叉的部分

ans =

     1     6
    31     7
  • Colon Operator 冒号操作
>> B = 1:5

B =

     1     2     3     4     5

>> B = 1:2:5

B =

     1     3     5

>> B = [1:5;2:3:15;-2:0.5:0]

B =

    1.0000    2.0000    3.0000    4.0000    5.0000
    2.0000    5.0000    8.0000   11.0000   14.0000
   -2.0000   -1.5000   -1.0000   -0.5000         0

>> str = 'a':2:'z'

str =

    'acegikmoqsuwy'
  • Indexing Using Colon Operator
>> A = [1 0 0; 5 0 0; 31 0 7]

A =

     1     0     0
     5     0     0
    31     0     7

>> A(3,:)

ans =

    31     0     7

>> A(3,:) = []

A =

     1     0     0
     5     0     0
  • Array Manipulation 数组操作
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UBdVvQ0R-1596526855854)(matlab718.png)]
>> x1 = A+a

x1 =

     3     4     5
     6     7     6
    11    10     9

>> x2 = A/a

x2 =

    0.5000    1.0000    1.5000
    2.0000    2.5000    2.0000
    4.5000    4.0000    3.5000

>> x3 = A./a      # 和上面结果一致

x3 =

    0.5000    1.0000    1.5000
    2.0000    2.5000    2.0000
    4.5000    4.0000    3.5000

>> x4 = A^a  # 即 A*A

x4 =

    36    36    32
    60    65    60
   104   114   108

>> x5 = A.^a

x5 =

     1     4     9
    16    25    16
    81    64    49

>> C = A'

C =

     1     4     9
     2     5     8
     3     4     7

右边那五个操作为矩阵操作,点乘为每个对应元素去做操作,除法为A/B = A*inv(B),即B的逆矩阵,详细参考矩阵操作

  • Array Concatenation 矩阵连接
>> D = [A B]

D =

     1     2     3     3     3     3
     4     5     4     2     4     9
     9     8     7     1     3     1

>> E = [A;B]

E =

     1     2     3
     4     5     4
     9     8     7
     3     3     3
     2     4     9
     1     3     1

特殊矩阵

  • linspace(a,b,c)
  • eye(n): nxn identity matrix:单位矩阵
  • zeros(n1,n2): n1xn2 zero matrix:零矩阵
  • ones(n1,n2): n1xn2 matrix with every entry as 1
  • diag(): diagonal matrix 对角矩阵
>> c = diag([2 3 4])

c =

     2     0     0
     0     3     0
     0     0     4

>> d = linspace(0,13,6)

d =

         0    2.6000    5.2000    7.8000   10.4000   13.0000

矩阵 function

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LkY5I9hK-1596526855856)(matlab7181.png)]
方法大多是对列column去做操作

>> A = [1 2 3;0 5 6;7 0 9]

A =

     1     2     3
     0     5     6
     7     0     9

>> max(A)  # 对每一列去求max,以下同理

ans =

     7     5     9

>> max(max(A))   # 求一个矩阵最大值的方法

ans =

     9

>> min(A)

ans =

     0     0     3

>> sum(A)

ans =

     8     7    18

>> mean(A)

ans =

    2.6667    2.3333    6.0000

>> sort(A)

ans =

     0     0     3
     1     2     6
     7     5     9

>> sortrows(A)

ans =

     0     5     6
     1     2     3
     7     0     9

>> size(A)

ans =

     3     3

>> length(A)

ans =

     3

>> find(A==0)

ans =

     2
     6

>> find(A==2)

ans =

     4

sort 和 sortrows区别是,sort对每一列进行排序,sortrows对第一列排序且 整行调换顺序


  • B站教程链接

台大郭彦甫matlab教程: 点击链接

猜你喜欢

转载自blog.csdn.net/weixin_44427114/article/details/107788207