MATLAB 基本操作与矩阵输入


前言

b站课程《MATLAB教程_台大郭彦甫(14课)》学习记录


第一部分

一、MATLAB as A Calculator

1. Operators: + - * / ^

  直接输入到命令行窗口,按ENTER直接算出结果

2. Result is computed, and displayed as ans

3. Precedence rules 优先规则

  1. Left-to-right within a precedence group

  2. Precedence groups are (highest first):

    1. Parenthesis ()
    2. Power (^)
    3. Multiplication and division (*, /)
    4. Addition and subtraction (+, -)
      在这里插入图片描述

二、Elementary Math Functions 基本数学函数

  1. Function list:
    http://www.mathworks.com/help/matlab/functionlist.html
    Arithmetic 算术
    Trigonometry 三角函数
    Exponents and Logarithms 指数和对数
    Complex Nembers 复数
    Cartesian Coordinate System Conversion 笛卡尔坐标系转换

三、Embedding Functions 嵌入函数

  1. Functions may be embedded into other functions
    在这里插入图片描述
  2. Many lines of code can be condensed into one single command
    许多行代码可以压缩成一个命令

四、Variables 变量

  1. Variables do NOT need to be declared before assignment

  2. A single “equal” sign (=) is the assignment operator:

     >> LHS = RHS
    
     >> A = 10
    
  3. Upper case/lower case make difference? Yes

  4. Can variable names can begin with a number? No

五、Numeric Variable (Data) Type 数值变量(数据)类型

可以在工作区直接双击变量名称查看变量的类型
或者在命令行窗口输入who & whos

>>who
您的变量为:
ans  
>> ans
ans =
   -0.8415
>> whos
  Name      Size         Bytes    Class     Attributes
  ans       1x1            8      double            

六、Special Variables and Constants 特殊变量和常数

  1. ans

  2. i , j : complex number

  3. Inf : ∞

  4. eps : 2.2204e-016(数字很小很小)

  5. NaN : not a number

  6. pi : Π

To list keywords:

    >>iskeyword
  >> 1/0

ans =

   Inf

>> log(0)

ans =

  -Inf

>> inf/inf

ans =

   NaN

七、MATLAB Calling Priority 命名优先级

在这里插入图片描述

cos = 'This string'

cos =

    'This string'

>> cos(8)

ans =

    'r'

cos 从三角函数变成了一个字符串

1. 把variable从工作区消除:clear + variable
2. clear 后面什么都不加的话是清除所有工作区的variable
3. Numeric Display “Format” 数字显示格式

在这里插入图片描述

>> pi

ans =

    3.1416

>> format long
>> pi

ans =

   3.141592653589793

>> format longE
>> pi

ans =

     3.141592653589793e+00
>> 3/13

ans =

     2.307692307692308e-01

>> format rat
>> 3/13

ans =

       3/13 

在这里插入图片描述

>> format long
>> 3/13+4/14+5/15

ans =

   0.849816849816850

>> format rat
>> 3/13+4/14+5/15

ans =

     232/273   

八、Command Line Terminal 命令行终端

Observe the difference between

>> a = 10

a =

    10

>> b = 10;
>> (不显示运算结果)

;】 at the end of a command suppresses output to the terminal 在命令的末尾,抑制输出到终端
(键盘上的)⬆】 display previous commands 显示之前的命令

九、Some Useful Functions

  1. clc : clear command window display
  2. clear: remove all variables in the workspace
  3. who: variables in the workspace
  4. whos: variable information of the workspace

第二部分

一、Array (Vector and Matrix)数组(向量和矩阵)

Row vector:(行向量)

    >> a = [1 2 3 4]

Column vector: (列向量)

    >> b = [1; 2; 3; 4]
>> a*b inner product 内积

ans =

      30         

>> b*a outer product 外积

ans =

       1              2              3              4       
       2              4              6              8       
       3              6              9             12       
       4              8             12             16      

A = [1 21 6;5 17 9;31 2 7]

A =

       1             21              6       
       5             17              9       
      31              2              7 

二、Array Indexing 数组索引

Select a certain subset of elements inside a matrix

>> A(8)  (以矩阵的每个列向量开始标号)

ans =

       9       

>> A([1 3 5]) (找出序号为1 3 5的数)

ans =

       1             31             17       

>> A([1 3;1 3])  变成 序号是1 3 和1 3 的矩阵

ans =

       1             31       
       1             31       

>> A(3,2)  找出三行二列的数字

ans =

       2       

>> A([1 3], [1 3])  前面是row 后面是column

第一个row和第三个row圈起来;第一个column和第三个column圈起来,得到的交集

ans =

       1              6       
      31              7       

三、Replacing Entries

Change the following elements in the matrix:
在这里插入图片描述

>> A = [1 21 6;5 17 9;31 2 7]

A =

       1             21              6       
       5             17              9       
      31              2              7       

>> A([4 6])=[76 0]

A =

       1             76              6       
       5             17              9       
      31              0              7       

OR

A([1 3],2)=[76;0]

A =

       1             76              6       
       5             17              9       
      31              0              7       

 
>> A([1 2], [2 3])=[0 0;0 0]

A =

       1              0              0       
       5              0              0       
      31              0              7       

四、Colon Operator 冒号运算符

Want to create a long array: A = [1 2 3 … 100]
Creates vectors or arrays, and specify for iterations 创建向量或数组,并指定迭代
Syntax: 句法规则
在这里插入图片描述

>> 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              2              3              4              5       
       2              5              8             11             14       
      -2             -3/2           -1             -1/2            0       

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

str =

    'acegikmoqsuwy'

五、Indexing Using Colon Operator 使用冒号运算符进行索引

在这里插入图片描述
How do we delete a row or a column of A?

>> A([1 2], [2 3])=[0 0;0 0]

A =

       1              0              0       
       5              0              0       
      31              0              7       

The expression
A( ,:)=[ ]
delete rows or columns of A

>> A(3,:) = [ ]
A =

       1              0              0       
       5              0              0 

六、Array Concatenation 数组连接

Arrays can be formed through concatenation as long as the rectangular shape is preserved
只要保持矩形形状,就可以通过串联形成数组

>> A = [1 2; 3 4]

A =

       1              2       
       3              4       

>> B = [9 9; 9 9]

B =

       9              9       
       9              9       

>> F = [A B]

F =

       1              2              9              9       
       3              4              9              9       

>> E = [A;B]

E =

       1              2       
       3              4       
       9              9       
       9              9   

在这里插入图片描述

>> A = [1 2; 3 4];
>> B = [9 9; 9 9];
>> C = [5 6 7 8];
>> D = [-2 -1 0 1];
>> F = [A B ; C; D]

F =

       1              2              9              9       
       3              4              9              9       
       5              6              7              8       
      -2             -1              0              1      

七、Array Manipulation 数组操作

Operators on array: + - * / ^ . ‘

  1. .*】对应位置元素相乘
  2. 【A/B】≈A*inv(B)
  3. ./】对应位置元素相除
  4. 矩阵与实数相加,每个元素上面都加上这个实数
  5. A/实数=A./实数
  6. A.^实数 为A中每个实数都变成实数次方
  7. 】转置
    在这里插入图片描述

A = [1 2 3;4 5 4;9 8 7];
B = [3 3 3;2 4 9;1 3 1];
a = 2;
x1 = A + a
x2 = A / a
x3 = A ./ a
x4 = A ^ a
x5 = A .^ a
C = A’
y1 = A + B
y2 = A * B
y3 = A .* B
y4 = A / B
y5 = A ./ B

x1 =

   3              4              5       
   6              7              6       
  11             10              9       

x2 =

   1/2            1              3/2     
   2              5/2            2       
   9/2            4              7/2     

x3 =

   1/2            1              3/2     
   2              5/2            2       
   9/2            4              7/2     

x4 =

  36             36             32       
  60             65             60       
 104            114            108       

x5 =

   1              4              9       
  16             25             16       
  81             64             49       

C =

   1              4              9       
   2              5              8       
   3              4              7       

y1 =

   4              5              6       
   6              9             13       
  10             11              8       

y2 =

  10             20             24       
  26             44             61       
  50             80            106       

y3 =

   3              6              9       
   8             20             36       
   9             24              7       

y4 =

   1/14           2/7            3/14    
   7/6            0              1/2     
 137/42          -2/7           -3/14    

y5 =

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

在这里插入图片描述

八、Some Special Matrix

linspace(): linearly spaced vectors 线性的向量
eye(n): n×n identity matrix n×n 单位矩阵
zeros(n1:n2): n1×n2 zero matrix n1×n2 零矩阵
ones(n1,n2): n1×n2 matrix with every entry as 1 n1×n2矩阵,每个条目为1
diag(): diagonal matrix 对角矩阵
rand(): uniformly distributed random numbers 均匀分布随机数

九、Some Matrix Related Functions

   max(A) 找出每列的最大的元素
   max(max(A)) 每列中最大的元素中最大的元素,即矩阵中最大的元素
   sum(A)每一列所有元素的加和
   mean(A)每一列元素的平均值
   sort(A)对每一列进行从小到大的排序
   sortrows(A)对第一列元素进行排序,带着行一起变化
   size(A) 判断A的Dimension多大,几个Row,几个Column
   length(A) 判断vector 向量的只有一个长度
   length是求某一矩阵所有维的最大长度
   find(A) 找到第几个等于某个数字的元素位置
>> A = [1 2 3; 0 5 6; 7 0 9]

A =

       1              2              3       
       0              5              6       
       7              0              9       

>> max(A)

ans =

       7              5              9       

>> max(max(A))

ans =

       9       

>> min(A)

ans =

       0              0              3       

>> sum(A)

ans =

       8              7             18       

>> mean(A)

ans =

       8/3            7/3            6       

>> 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 == 5)

ans =

       5       

>> find(A == 6)

ans =

       8       

END


总结

继续加油吧

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值