美文网首页
二、基本操作与矩阵输入

二、基本操作与矩阵输入

作者: 看远方的星 | 来源:发表于2021-06-23 12:17 被阅读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
  • keywords:
    ans:answer
    i,j:complex number
    Inf:\infty 无限大
    eps:2.2204e-016 很小很小的数
    NaN:not a number
    pi:π

  • To list keywords:

>>iskeyword
  • What's the answer from MATLAB after typing?
>>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
  • The expression: A()=[ ]
    delete rows or columns of A
    分号:表示整行或整列
    A(3, :)表示第三行全部列
    A(3, :)=[]表示第三行全部列为空
>> A
A =
     1     0     0
     5     0     0
    31     0     7

>> A(3, :)=[]
A =
     1     0     0
     5     0     0
Colon(分号:) Operator
  • Want to create a long array:A=[123.…100]
  • Creates vectors or arrays, and specify for iterations
  • Syntax:
    image.png
    A=[第一个数,间距,最后一个数 ]
    无间距默认间距为1
    因为间距问题,最后一个数未必有

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
  • Arrays can be formed through concatenation as long as the rectangular shape is preserved
image.png
  • Create matrices A,B,C,and D and concatenate(联系起来) them into F:


    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
  • Operators on array:+ - * / ^ . '
  • Type the following command and observe the results:


    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
  • linspace():linearly spaced vectors(线性间隔的向量)
  • eye(n): n x n identity matrix(单位矩阵)
  • zeros(n1,n2):n1xn2 zero matrix (全是零的矩阵)
  • ones(n1,n2):n1xn2 matrix with every entry as 1(全是1的矩阵)
  • diag():diagonal matrix(对角线矩阵,对角线有值,其他都没有值)
  • rand():uniformly distributed random numbers
  • Type the following command and observe the results:
    >>linspace(0,13,6)
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

相关文章

  • 二、基本操作与矩阵输入

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

  • MATLAB:基本操作和矩阵输入

    四则运算:(^,*,/,+,-) 不会使用的功能和函数通过:help+函数/功能查询。 函数嵌套。 变量 变量不可...

  • Numpy

    操作数组和矩阵 基本操作 二维操作 特殊操作 属于np的函数 平均值 索引切片 打印行列 行矩阵 多个矩阵合并 ...

  • NumPy学习

    矩阵操作 基本操作 向量 类型 取值 判断 矩阵 维度 求值 比较 特定赋值 类型转换 求最值 矩阵操作 生成序列...

  • 基本矩阵操作

    2.2.1、矩阵和数组的概述 矩阵是matlab中重要的内建数据结构,对于矩阵的操作主要包括:矩阵的构建,维度和大...

  • 矩阵和向量

    概述 本文主要讲解矩阵和向量的基础数学知识,我们的目标是能掌握基本的运算操作即可。 矩阵 向量 矩阵运算 矩阵与向...

  • Octave基础教程

    一、基本操作 数学运算与逻辑运算 数学运算 逻辑运算 向量与矩阵 矩阵 向量 注:构造过程中用“;”分隔,不要用成...

  • 基础矩阵、本质矩阵,单应矩阵及其解法

    本质矩阵,基础矩阵,单应矩阵,自由度及其解法基本矩阵、本质矩阵和单应矩阵基本矩阵的基本解法之8点算法单应矩阵与基础...

  • 三 ndarray 数据基本操作

    ndarray 数据基本操作 (1)数组与标量、数组之间的运算 (2)数组的矩阵积(matrix produ...

  • NumPy Tips

    在机器学习领域中,NumPy是最基本的数据结构,用于存储矩阵和执行与矩阵计算相关的操作。本文主要分享关于NumPy...

网友评论

      本文标题:二、基本操作与矩阵输入

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