MATLAB_2

2022/4/28 23:44:12

本文主要是介绍MATLAB_2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

%% MATLAB as A Calculator(MATLAB 作为计算器)
%Operators(操作符):+ - * /
%Result is computed,and displayed as ans(结果由计算机显示为 ans)
%Precedence rules(优先规则):
%Left-to-right within a precedence group(在优先组中从左到右)
%Precedence groups are (highest first)(优先级组为(最高优先)):
%Parenthesis() 括号
%Power(^) 幂
%Multiplication and division(,/) 乘除
%Addition and subtraction(+,-) 加减
%Exercise
%calculate:
cos(((1+2+3+4)3/5)0.5)%平分根就是0.5次方
cos(sqrt((1+2+3+4)^3/5))%sqrt也是开平分根
sin(pi^0.5)+log(tan(1))%ln就是log
2^(3.5
1.7)
exp(sin(10))%e=exp(1),不是e^(sin(10)),e无法识别
%% Embedding Function(嵌入功能)
%Functions may be embedded into other functions(函数可以嵌入到其他函数中)
sin(cos(pi))
%<=>
cos(pi)
sin(ans)
%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(一个“等号(=)是赋值运算符):
%upper case/lower case make difference(大写/小写有区别)
%Variable names can not begin with a number(名字开头不能是数字)
%% Numeric Variable (Data) Type(数值变量(数据)类型)
%tips:
%指令who可以看到有哪些变量
%指令whos可以看到有哪些变量及其Size Bytes Class Attributes
%% Special Variables and Constants(特殊变量和常量)
%ans 是matlab关键字
%i,j:complex number(复数),虚数运算时使用
%Inf:∞ 无限大
%例:
1/0
%eps:2.2204e-016 极小的数
%例:
x=log(0)
%NaN:not a number
%例:
x=inf/inf
%pi:π

%关键字不能当作变量

%tips:
    %查看matlab关键字:iskeyword

%% MATLAB Calling Priority(MATLAB调用优先)
%High
%↓Variable(变量)
%↓Built-in fuction(内置函数)
%↓Subfunction(子功能)
%↓Private function(私有函数):
%MEX-file
%P-file
%M-file
%Low
%例:
cos='hello world'
cos(8)
%ans ='o',而不是执行cos函数计算cos8
%Variable(变量)优先级比Built-in function(内置函数)高,所以先处理的是cos='hello world'
%tips:
%不要用built-function name或keyword当作variable name
%如果用了,可以用 clc name 来清除这个变量,不要用成 clc,会消光所有变量
%% Numeric Display "Format"(数字显示“格式”)
%例:
format long
pi %ans=3.141592653589793
%short
%{
Short,fixed-decimal format with 4 digits after the decimal point.
(短定点格式.显示小数点后4位)
例:3.1416
%}
%long
%{
Long,fixed-decimal format with 15 digits after the decimal point
for double values,and 7 digits after the decimal point for single
values.
(长定点格式.对double类型变量显示小数点后15位,对float类型变量显示小数点后7位.)
例:3.141592653589793
%}
%shortE
%{
Short scientific notation with 4 digits after the decimal
point.
(短科学计数法,显示小数点后4位.并带有科学计数法标记.)
例:3.1416e+00
%}
%longE
%{
Short scientific notation with 15 digits after the decimal point
fordouble values,and 7 digits after the decimal point for
single values.
(长科学计数法.对double类型变量显示小数点后15位,对float类型变量显示小数点后7位.并带有科学计数法标记.)
例:3.141592653589793e+00
%}
%bank
%{
Currency format with 2 digits after the decimal point.
(银行格式.显示小数点后2位.)
例:3.14
%}
%hex
%{
Hexadecimal representation of a binary double-precision number.
十六进制格式.
例:400921fb54442d18
%}
%rat
%{
Ratio of small integers.
(比例格式)
例:355/113
%}
%% Command Line Terminal(命令行终端)
%Observe the difference between
a=10
a=10;
%';' at the end of a command suppresses output to the terminal
%tips:↑ 可以用来显示以前的命令
%% Some Functions(一些功能)
%clc:clera command window display1(清除终端的输出)
%clear:remove all variables in the workspace(清除当前工作区内所有变量)
%who:variables in the workspace(清除当前工作区内所有变量)
%whos:variable information of the workspace(以复杂格式显示工作区内所有变量)
%% Array(Vector an Matrix)(数组(向量矩阵))
%Row vector(行向量):
a=[1 2 3 4]%space换成','也行
%Column vector(列向量):
b=[1;2;3;4]
%例
ab %ans=30
b
a %ans =
%1 2 3 4
%2 4 6 8
%3 6 9 12
%4 8 12 16
%Enter a matrix in MATLAB:
A=[1,2,3;4,5,6;7,8,9]
%% array Indexing(数组索引)
%Select a certain subset of elements inside a matrix
%For row vector:
a=[-1 -2 -3 -4]
a(3)
%ans=-3
%For column vector
b=[-1;-2;-3;-4]
b(4)
%ans=-4
%For matrix
A=[1,2,3;4,5,6;7,8,9]
%法1:
A(1,2)%ans=2
%第一行与第二列交叉的元素
A([1 3],[1,3])%ans=
%1 3
%7 9
%第一个中括号是row,第二个中括号是column,第1,3行与第1,3列交叉的元素
%法2:
A(1)%ans=1
%第1个元素
A(6)%ans=8
%第8个元素,竖着按列数的,不是按行
A([1 3 5])%ans=1 7 5
A([1,3;1,3])%ans =
%1 7
%1 7
%';'分行了,有两行,第一行是第一个和第三个元素,第二行是第一个第三个元素
%% Replacing Entries
%Change the following elements in the matrix(更改矩阵中的以下元素):
A=[1,2,3;4,5,6;7,8,9]%=>%1 2 3
%4 5 6
%7 8 9
A(1,2)=20
%=============>%1 20 3
%把(1,2)位置上的2取代为20 %4 5 6
%7 8 9
A(3,:)=[]
%
=>%1 20 3
%把第三行为空 %4 5 6

%% Colon Operator
%want to create a long array:A=[1 2 3 ... 100]
%Creates vectors or arrays,and specify for iterations(创建向量或数组,并指定迭代)
%Syntax(语法):
%j:k=>[j,j+1,j+2, ...,j+m]
%j:i:k=>=>[j, j+1, j+2i, ..., j+mi]
%j:k相当于i=1,j+m
i是小于等于k的最大值
%例
A=1:5
B=1:2:5
C=[1:5; 2:3:15; -2:0.5:0]%要串联的数组的维度要 一致
str='a':2:'z'
%% Array Manipulation(数组操作)
%Opearators on array: + - * / ^ . '
%例
A=[1 2 3; 4 5 4; 9 8 7]
B=[3 3 3; 2 4 9; 1 3 1]
a=2
%+
x=A+a
%{
x =
3 4 5
6 7 6
11 10 9
(矩阵与向量相加)
%}
y =A+B
%{
y =
4 5 6
6 9 13
10 11 8
(矩阵与矩阵对应位置相加)
%}
%-
x =A-a
%{
x =
-1 0 1
2 3 2
7 6 5
(矩阵与向量相减)
%}
y =A-B
%{
y =
-2 -1 0
2 1 -5
8 5 6
(矩阵与矩阵对应位置相减)
%}
%(矩阵与矩阵相乘)
x =A
B
%{
x =
10 20 24
26 44 61
50 80 106
%}
%.(矩阵与矩阵对应位置相乘)
x =A.
B
%{
x =
3 6 9
8 20 36
9 24 7
%}
%/(矩阵与矩阵右除)
x =A/B
%{
x =
0.0714 0.2857 0.2143
1.1667 0 0.5000
3.2619 -0.2857 -0.2143
%}
%(矩阵与矩阵左除)
x =A/B
%{
x =
0.0714 0.2857 0.2143
1.1667 0 0.5000
3.2619 -0.2857 -0.2143
%}
%./(矩阵与矩阵对应位置右除)
x =A./B
%{
x =
0.3333 0.6667 1.0000
2.0000 1.2500 0.4444
9.0000 2.6667 7.0000
%}
%.(矩阵与矩阵对应位置左除)
x =A.\B
%{
x =
3.0000 1.5000 1.0000
0.5000 0.8000 2.2500
0.1111 0.3750 0.1429
%}
%^(矩阵与向量乘方)
x =A^a
%{
x =
36 36 32
60 65 60
104 114 108
%}
%.^(矩阵与矩阵对应位置乘方)
x =A.^B
%{
x =
1 8 27
16 625 262144
9 512 7
%}
%.'(矩阵的转置)
x =A'
%{
x =
1 4 9
2 5 8
3 4 7
%}
%% Some Special Matrix(一些特殊矩阵)
%linspace()
%{
linspace(x1,x2):生成x1到x2之间的100个等间距点的行向量
linspace(x1,x2,n):生成x1到x2之间的n个等间距点的行向量
%}
%eye(n):生成n行n列的对角线上全是1的矩阵
%zeros(n1,n2):生成n1行n2列的全是0的矩阵
%ones(n1,n2):生成n1行n2列的全是1的矩阵
%diag():生成对角线上是方括号内部数据的矩阵。
%rand():生成n1行n2列随机数矩阵,生成的数为0-1之间
%% Some Matrix Related Function
% max(A) :找到A矩阵里每一列的最大值,组成一个行向量
%{
max(A') :找到A矩阵里每一行的最大值,组成一个行向量
max(max(A)):找到A矩阵里每一列的最大值,组成一个行向量,在这一个行向量里,找到最大的值
%}
% min(A) :找到A矩阵里每一列的最小值,组成一个行向量
% sum(A) :算出A矩阵里每一列的和,组成一个行向量
% mean(A) :算出A矩阵里每一列的平均值,组成一个行向量
% sort(A) :将每一列从小排到大
% sortrows(A):每一行作为一整体,根据第一列从小到大,其它列整体移动
% size(A) :矩阵A有多少行和多少列
% length(A) :在矩阵A中,如果行比列多,则返回行数,如果列比行多,则返回列数。也就是返回行和列中多的一个
% find(A) :寻找矩阵A中非零元素的位置是多少
% find(A==5) :返回矩阵A中等于5的位置,注意,是位置,而不是数。



这篇关于MATLAB_2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程