二、基本操作与矩阵输入

2021-06-23  本文已影响0人  看远方的星

Today:
一、Introduction
二、MATLAB as calculator
三、Array(矩阵或向量) operation

一、Introduction(略)

二、MATLAB as A Calculator

Operators: + - * / ^
Result is computed, and displayed as ans
Precedence rules:
· Left-to-right within a precedence group
· Precedence groups are(highest first):
1. Parenthesis() 圆括号
2. Power(^) 次方
3. Multiplication and division(*,/)
4. Addition and subtraction(+,-)

image.png

不会怎么办:谷歌,命令行:help cos,search框

答案:

>> cos(sqrt((1+2+3+4)^3/5))
ans =
   -0.0050

>> sin(pi^0.5)+log(tan(1))
ans =
    1.4228

>> 2^(3.5*1.7)
ans =
   61.8199

>> exp(sin(10))
ans =
    0.5804

>> 2.7183^(sin(10))  # e约等于2.7183
ans =
    0.5804
Elementary Math Functions

Function list: http://www.mathworks.com/help/matlab/functionlist.html
Arithmetic 算术
Trigonometry 三角函数
Exponents and Logarithms 指数和对数
Complex Numbers 复数
Cartesian Coordinate System Conversion 笛卡尔坐标系转换
在function list 里搜索以上关键词即可

Embedding Functions

Functions may be embedded into other functions,

image.png

Many lines of code can be condensed into one single command

Variables

·Variables do NOT need to be declared before assignment(指定前不需声明)
·A single "equal"sign(=)is the assignment operator:


image.png
a = 10 #可以
10 = a #不可以

1.Upper case/lower case make difference?
大写变量和小写变量不一样,可以同时存在

# matlab的尝试:
>> A = 10

A =
    10

>> a = 20

a =
    20

2.Can variable names can begin with a number?
变量名不能以数字开头,但是可以包含数字

Numeric Variable(Data)Type
image.png

workspace双击变量名可看到变量类型。

命令 :who 看变量,whos看更详细的变量

>> who
Your variables are:
A  a  

>> whos
  Name      Size            Bytes  Class     Attributes

  A         1x1                 8  double              
  a         1x1                 8  double  
Special Variables and Constants
>>iskeyword
>>x=1/0
>>x=1og(0)
>>x=inf/inf
>> x=1/0
x =
   Inf

>> log(0)
ans =
  -Inf

>> x=inf/inf  #无限大除无限大在数学上没有被定义
x =
   NaN
MATLAB Calling Priority
image.png
>> cos='this string'  #变量优先级高于函数
cos =
this string

>> cos(8)  #函数被变量覆盖
ans =
r

不要用关键词或built-in function name(内置函数名)做变量名

如果使用了,可用clear消除变量,clear后面不加变量就消除所有变量!!

>> clear cos
>> cos(8)
ans =
   -0.1455
Numeric Display "Format"

format long:指定数字格式显示为long style

image.png
bank显示两个小数(与美元数值相关)
hex显示16进制
rat显示有理数
>> pi
ans =
    3.1416

>> format long

>> pi
ans =
   3.141592653589793
Exercise
image.png

有理数时答案是1234哪个?1
小数时答案是abcd哪个?d

>> format rat

>> 3/13+4/14+5/15
ans =
     232/273   

>> format long

>> 3/13+4/14+5/15
ans =
   0.849816849816850

Command Line Terminal

·Observe the difference between

>>a=10
>>b=10;

·at the end of a command suppresses(抑制) output to the terminal(命令后加分号,不显示运算结果)
·个(向上的箭头)display previous commands(向上的箭头查看之前运行命令)

Some Functions

clc:clear command window display
clear:remove all variables in the workspace
who:variables in the workspace
whos:variable information of the workspace

三、Array(矩阵或向量) operation

Array(Vector and Matrix)

·Row vector:
>>a=[1 2 3 4]
·Column vector:
>>b=[1;2;3;4] (分号类似换行符)
·Try:

>>a*b
>>b*a
·Key in the following matrix in MATLAB: image.png
>> a=[1 2 3 4]
a =
     1     2     3     4

>> b=[1;2;3;4]
b =
     1
     2
     3
     4

>> a*b
ans =
    30

>> b*a
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


image.png

两种方法:
一是小括号内有逗号:A(1,2)表示第一行第二列元素
二是小括号内无逗号:A(5)表示17,序号顺序如下:


image.png

What's the answer from MATLAB after typing?

#方法二:
>>A(8)
>>A([1 3 5])
>>A([1 3;1 3])
#方法一:
>>A(3,2)
>>A([1 3],[1 3])
>> 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])  #见下图解释
ans =
     1     6
    31     7
image.png
Replacing Entries

·Change the following elements in the matrix:

image.png
>> A(1,3)=76
A =
     1    21    76
     5    17     9
    31     2     7

>> A(3,2)=0
A =
     1    21    76
     5    17     9
    31     0     7

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

>> A(2,3)=0
A =
     1     0     0
     5     0     0
    31     0     7
>> A
A =
     1     0     0
     5     0     0
    31     0     7

>> A(3, :)=[]
A =
     1     0     0
     5     0     0
Colon(分号:) Operator

What's the answer from MATLAB after typing?

>>B=1:5
>>B=1:2:5
>>B=[1:5;2:3:15;-2:0.5:0]
>>str='a':2:'z'
>> 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
Array Concatenation
image.png
>> A=[1 2; 3 4];
>> B=[9 9; 9 9];
>> F=[A B]
F =
     1     2     9     9
     3     4     9     9

>> F=[A;B]
F =
     1     2
     3     4
     9     9
     9     9
Array Manipulation(处理):矩阵的运算
image.png
>> A=[1 2 3;4 5 4; 9 8 7];
>> B=[3 3 3;2 4 9; 1 3 1];
>> a=2;
>> y1=A+B   # 各个位置相加到原来的位置上
y1 =
     4     5     6
     6     9    13
    10    11     8

>> y2=A*B  # 第一行乘第一列相加
y2 =
    10    20    24
    26    44    61
    50    80   106

>> y3=A.*B  # 点乘:各个位置相乘到原来位置上
y3 =
     3     6     9
     8    20    36
     9    24     7

>> y4=A/B # A乘以 inv(B)与结果大概相等
y4 =
    0.0714    0.2857    0.2143
    1.1667         0    0.5000
    3.2619   -0.2857   -0.2143

>> y5=A./B  # 点除:各个位置相除到原来位置上
y5 =
    0.3333    0.6667    1.0000
    2.0000    1.2500    0.4444
    9.0000    2.6667    7.0000

>> x1=A+a  # 每个位置都加上a,a为2
x1 =
     3     4     5
     6     7     6
    11    10     9

>> x1=A/a  # 每个位置都除以a
x1 =
    0.5000    1.0000    1.5000
    2.0000    2.5000    2.0000
    4.5000    4.0000    3.5000

>> x1=A./a   # 每个位置都除以a,与上面的除法一致
x1 =
    0.5000    1.0000    1.5000
    2.0000    2.5000    2.0000
    4.5000    4.0000    3.5000

>> x4=A^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' # A的转置
c =
     1     4     9
     2     5     8
     3     4     7
image.png
Some Special Matrix
Some Matrix Related Functions
image.png

Type the following command and observe the results:


image.png
>> 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 =
    2.6667    2.3333    6.0000

>> sort(A) # 按列排序
ans =

     0     0     3
     1     2     6
     7     5     9

>> sortrows(A)  
# 默认按照第一列大小给整行排序,第二列的话:sortrows(A,2)
# 降序:sortrows(A,-2)
ans =

     0     5     6
     1     2     3
     7     0     9

>> size(A) #有几行有几列?
ans =
     3     3

>> length(A)  # 一个维度,所以只有长度,没有宽
ans =
     3

>> find(A==6)  # A的第几个位置等于6
ans =
     8
上一篇 下一篇

猜你喜欢

热点阅读